summaryrefslogtreecommitdiff
path: root/addons/websocket/nx_websocket_client.c
blob: 5b4de83fda958f3b1cbaefc71da60f9cc7b2f8da (plain)
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present 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
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */ 
/** NetX WebSocket Component                                              */
/**                                                                       */
/**   WebSocket Protocol                                                  */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_WEBSOCKET_CLIENT_SOURCE_CODE

/* Force error checking to be disabled in this module */

#ifndef NX_DISABLE_ERROR_CHECKING
#define NX_DISABLE_ERROR_CHECKING
#endif

/* Include necessary system files.  */

#include "tx_api.h"
#include "nx_ip.h"
#include "nx_packet.h"
#include "nx_tcp.h"
#include "nx_websocket_client.h"


/* Bring in externs for caller checking code.  */

NX_CALLER_CHECKING_EXTERNS

#define NX_WEBSOCKET_CRLF                           "\r\n"
#define NX_WEBSOCKET_CRLF_SIZE                      2

#define NX_WEBSOCKET_HEADER_MINIMUM_LENGTH          2

#define NX_WEBSOCKET_ACCEPT_PREDEFINED_GUID         "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
#define NX_WEBSOCKET_ACCEPT_PREDEFINED_GUID_SIZE    (sizeof(NX_WEBSOCKET_ACCEPT_PREDEFINED_GUID) - 1)
#define NX_WEBSOCKET_ACCEPT_DIGEST_SIZE             20 /* The length of SHA-1 hash is 20 bytes */
#define NX_WEBSOCKET_ACCEPT_KEY_SIZE                28 /* The base64 encode key for 20 bytes digest requires (27 bytes name size + 1 byte pad) = 28 bytes */

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_create                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket instance create    */
/*    function call.                                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    client_name                           Name of this WebSocket        */
/*    ip_ptr                                Pointer to IP instance        */ 
/*    pool_ptr                              Pointer to packet pool        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_create           Actual websocket create call  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_create(NX_WEBSOCKET_CLIENT *client_ptr, UCHAR *client_name, NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr)
{

UINT        status;

    /* Check for invalid input pointers.  */
    if ((ip_ptr == NX_NULL) || (ip_ptr -> nx_ip_id != NX_IP_ID) || 
        (client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id == NX_WEBSOCKET_CLIENT_ID) || 
        (pool_ptr == NX_NULL) || (pool_ptr -> nx_packet_pool_id != NX_PACKET_POOL_ID))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual client create function.  */
    status = _nx_websocket_client_create(client_ptr, client_name, ip_ptr, pool_ptr);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_create                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates an instance for WebSocket Client.             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    client_name                           Name of this WebSocket        */
/*    ip_ptr                                Pointer to IP instance        */ 
/*    pool_ptr                              Pointer to packet pool        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_create(NX_WEBSOCKET_CLIENT *client_ptr, UCHAR *client_name, NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr)
{

UINT status;

    /* Clear the WebSocket structure.  */
    memset((void *)client_ptr, 0, sizeof(NX_WEBSOCKET_CLIENT));

    /* Create WebSocket mutex.  */
    status = tx_mutex_create(&client_ptr -> nx_websocket_client_mutex, (CHAR *)client_name, TX_NO_INHERIT);
    if (status)
    {
        return(status);
    }

    /* Save the Client name.  */
    client_ptr -> nx_websocket_client_name = client_name;

    /* Save the IP pointer address.  */
    client_ptr -> nx_websocket_client_ip_ptr = ip_ptr;

    /* Save the packet pool pointer.  */
    client_ptr -> nx_websocket_client_packet_pool_ptr = pool_ptr;

    /* Set the Client ID to indicate the WebSocket client thread is ready.  */
    client_ptr -> nx_websocket_client_id = NX_WEBSOCKET_CLIENT_ID;

    /* Update the state.  */
    client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_IDLE;

    /* Return successful completion.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_delete                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket instance delete    */
/*    function call.                                                      */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_delete           Actual websocket delete call  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_delete(NX_WEBSOCKET_CLIENT *client_ptr)
{

UINT        status;

    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual client delete function.  */
    status = _nx_websocket_client_delete(client_ptr);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_delete                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function deletes an instance for WebSocket Client.             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    _nx_websocket_client_cleanup          Cleanup unused resources      */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_delete(NX_WEBSOCKET_CLIENT *client_ptr)
{

    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Clean up unused resources.  */
    _nx_websocket_client_cleanup(client_ptr);

    /* Set the Client ID to a default value.  */
    client_ptr -> nx_websocket_client_id = 0;

    /* Update the state.  */
    client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_INITIALIZE;

    /* Release the mutex, delete the mutex and assign the pointer to NULL. */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
    tx_mutex_delete(&(client_ptr -> nx_websocket_client_mutex));

    /* Return successful completion.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_connect                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket connect.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    socket_ptr                            Pointer to TCP socket         */
/*    host                                  Pointer to host               */ 
/*    host_length                           Length of host                */
/*    uri_path                              Pointer to uri path           */ 
/*    uri_path_length                       Length of uri path            */
/*    protocol                              Pointer to protocol           */ 
/*    protocol_length                       Length of protocol            */
/*    bearer                                Pointer to bearer             */ 
/*    bearer_length                         Length of bearer              */
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_connect          Actual websocket connect call */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_connect(NX_WEBSOCKET_CLIENT *client_ptr, NX_TCP_SOCKET *socket_ptr,
                                    UCHAR *host, UINT host_length,
                                    UCHAR *uri_path, UINT uri_path_length,
                                    UCHAR *protocol, UINT protocol_length,
                                    UCHAR *bearer, UINT bearer_length, UINT wait_option)
{

UINT        status;


    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID) || 
        (socket_ptr == NX_NULL) || (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID) ||
        (host == NX_NULL) || (host_length == 0) || 
        (uri_path == NX_NULL) || (uri_path_length == 0))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual connect function.  */
    status = _nx_websocket_client_connect(client_ptr, socket_ptr, host, host_length, uri_path, uri_path_length, protocol, protocol_length, bearer, bearer_length, wait_option);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_connect                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function makes a WebSocket connection over TCP socket to the   */
/*    server.                                                             */
/*    Note: Application must establish a TCP connection before.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    socket_ptr                            Pointer to TCP socket         */
/*    host                                  Pointer to host               */ 
/*    host_length                           Length of host                */
/*    uri_path                              Pointer to uri path           */ 
/*    uri_path_length                       Length of uri path            */
/*    protocol                              Pointer to protocol           */ 
/*    protocol_length                       Length of protocol            */
/*    bearer                                Pointer to bearer             */ 
/*    bearer_length                         Length of bearer              */
/*    wait_option                           Wait option                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_connect_internal Make websocket connection     */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_connect(NX_WEBSOCKET_CLIENT *client_ptr, NX_TCP_SOCKET *socket_ptr,
                                   UCHAR *host, UINT host_length,
                                   UCHAR *resource, UINT resource_length,
                                   UCHAR *protocol, UINT protocol_length,
                                   UCHAR *bearer, UINT bearer_length, UINT wait_option)
{

UINT status;


    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Check the state.  */
    if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_CONNECTED)
    {

        /* Release the mutex and return */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_ALREADY_CONNECTED);
    }
    else if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_CONNECTING)
    {

        /* Release the mutex and return */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_CONNECTING);
    }
    else if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_INITIALIZE)
    {

        /* Release the mutex and return */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_INVALID_STATE);
    }

    /* Save the socket pointer.  */
    client_ptr -> nx_websocket_client_socket_ptr = socket_ptr;

#ifdef NX_SECURE_ENABLE
    client_ptr -> nx_websocket_client_use_tls = NX_FALSE;
#endif /* NX_SECURE_ENABLE */

    status = _nx_websocket_client_connect_internal(client_ptr, host, host_length, resource, resource_length, protocol, protocol_length, bearer, bearer_length,  wait_option);

    /* Release the mutex and return */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_connect_internal               PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function makes a WebSocket connection over TCP socket or TLS   */
/*    session to the server.                                              */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    host                                  Pointer to host               */ 
/*    host_length                           Length of host                */
/*    uri_path                              Pointer to uri path           */ 
/*    uri_path_length                       Length of uri path            */
/*    protocol                              Pointer to protocol           */ 
/*    protocol_length                       Length of protocol            */
/*    bearer                                Pointer to bearer             */ 
/*    bearer_length                         Length of bearer              */
/*    wait_option                           Wait option                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_packet_allocate                    Allocate a packet             */
/*    nx_packet_release                     Release the packet            */
/*    nx_secure_tls_packet_allocate         Allocate a TLS packet         */
/*    nx_packet_data_append                 Append data                   */
/*    _nx_utility_base64_encode             Base64 encode                 */
/*    _nx_websocket_client_packet_send      Send out websocket packet     */
/*    _nx_websocket_client_packet_receive   Receive a websocket packet    */
/*    _nx_websocket_client_connect_response_check                         */
/*                                          Process connect response      */
/*    _nx_websocket_client_cleanup          Cleanup resources             */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_connect          Make websocket connection     */
/*    _nx_websocket_client_secure_connect   Make secure websocket         */
/*                                            connection                  */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_connect_internal(NX_WEBSOCKET_CLIENT *client_ptr,
                                            UCHAR *host, UINT host_length,
                                            UCHAR *uri_path, UINT uri_path_length,
                                            UCHAR *protocol, UINT protocol_length,
                                            UCHAR *bearer, UINT bearer_length, UINT wait_option)
{

UINT i;
UINT status;
NX_PACKET *packet_ptr;


    /* To guarantee resources are cleaned-up if a re-connect happens. */
    _nx_websocket_client_cleanup(client_ptr);

    /* Generate GUID for WebSocket Key.  */
    for (i = 0; i < NX_WEBSOCKET_CLIENT_GUID_SIZE; i ++)
    {
        client_ptr -> nx_websocket_client_guid[i] = (UCHAR)(NX_RAND());
    }

    /* Encode the GUID as key.  */
    _nx_utility_base64_encode(client_ptr -> nx_websocket_client_guid, NX_WEBSOCKET_CLIENT_GUID_SIZE, 
                              client_ptr -> nx_websocket_client_key, NX_WEBSOCKET_CLIENT_KEY_SIZE,
                              &client_ptr -> nx_websocket_client_key_size);

    /* Release the mutex */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));

    /* Allocate a packet.  */
#ifdef NX_SECURE_ENABLE
    if (client_ptr -> nx_websocket_client_use_tls)
    {

        /* Use TLS packet allocate.  The TLS packet allocate is able to count for
           TLS-related header space including crypto initial vector area. */
        status = nx_secure_tls_packet_allocate(client_ptr -> nx_websocket_client_tls_session_ptr,
                                               client_ptr -> nx_websocket_client_packet_pool_ptr,
                                               &packet_ptr, TX_WAIT_FOREVER);
    }
    else
    {
#endif /* NX_SECURE_ENABLE */

        /* Allocate packet.  */
        if (client_ptr -> nx_websocket_client_socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
        {
            status =  nx_packet_allocate(client_ptr -> nx_websocket_client_packet_pool_ptr,
                                         &packet_ptr,
                                         NX_IPv4_TCP_PACKET, wait_option);
        }
        else
        {
            status =  nx_packet_allocate(client_ptr -> nx_websocket_client_packet_pool_ptr,
                                         &packet_ptr,
                                         NX_IPv6_TCP_PACKET, wait_option);
        }
#ifdef NX_SECURE_ENABLE
    }
#endif /* NX_SECURE_ENABLE */

    /* Check status. */
    if (status)
    {

        /* Obtain the mutex again and return error status. */
        tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);
        return(status);
    }

    /* WebSocket opening handshake message format:
       GET /mqtt HTTP/1.1\r\n
       Host: example.com\r\n
       Upgrade: websocket\r\n
       connection: Upgrade\r\n
       Sec-WebSocket-Key: xxxxxxx=\r\n
       Sec-WebSocket-Protocol: mqtt\r\n
       Sec-WebSocket-Version: 13\r\n
       \r\n
    */

    /* Build the GET request: Get + Request URI + HTTP version.  */
    status =  nx_packet_data_append(packet_ptr, "GET ", sizeof("GET ") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, uri_path, uri_path_length, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, " HTTP/1.1", sizeof(" HTTP/1.1") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Place the Host in the header.  */
    status += nx_packet_data_append(packet_ptr, "Host: ", sizeof("Host: ") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, host, host_length, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Place the Upgrade in the header.  */
    status += nx_packet_data_append(packet_ptr, "Upgrade: websocket", sizeof("Upgrade: websocket") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Place the Connection in the header.  */
    status += nx_packet_data_append(packet_ptr, "Connection: Upgrade", sizeof("Connection: Upgrade") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Place the Sec-WebSocket-Key in the header.  */
    status += nx_packet_data_append(packet_ptr, "Sec-WebSocket-Key: ", sizeof("Sec-WebSocket-Key: ") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, client_ptr -> nx_websocket_client_key, client_ptr -> nx_websocket_client_key_size, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Place the Sec-WebSocket-Protocol in the header.  */
    if ((protocol != NX_NULL) && (protocol_length != 0))
    {
        status += nx_packet_data_append(packet_ptr, "Sec-WebSocket-Protocol: ", sizeof("Sec-WebSocket-Protocol: ") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
        status += nx_packet_data_append(packet_ptr, protocol, protocol_length, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
        status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    }

    /* Place the Sec-WebSocket-Version in the header.  */
    status += nx_packet_data_append(packet_ptr, "Sec-WebSocket-Version: 13", sizeof("Sec-WebSocket-Version: 13") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Place the Bearer in the header.  */
    if ((bearer != NX_NULL) && (bearer_length != 0))
    {
        status += nx_packet_data_append(packet_ptr, "Authorization: Bearer ", sizeof("Authorization: Bearer ") - 1, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
        status += nx_packet_data_append(packet_ptr, bearer, bearer_length, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
        status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);
    }

    /* Fill the last \r\n.  */
    status += nx_packet_data_append(packet_ptr, NX_WEBSOCKET_CRLF, NX_WEBSOCKET_CRLF_SIZE, client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

    /* Check status.  */
    if (status)
    {

        /* Obtain the mutex, release the packet and return error status. */
        tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);
        nx_packet_release(packet_ptr);
        return(NX_WEBSOCKET_DATA_APPEND_FAILURE);
    }

    /* Send out the packet.  */
    status = _nx_websocket_client_packet_send(client_ptr, packet_ptr, wait_option);
    if (status)
    {

        /* Release the packet and return error status. */
        nx_packet_release(packet_ptr);
        return(status);
    }

    /* Obtain the mutex again. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Update the subprotocol name and length */
    client_ptr -> nx_websocket_client_subprotocol = protocol;
    client_ptr -> nx_websocket_client_subprotocol_length = protocol_length;

    /* Update the state.  */
    client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_CONNECTING;

    /* Set the frame flag to be unfragmented and corresponding opcode to be zero */
    client_ptr -> nx_websocket_client_frame_fragmented = NX_FALSE;
    client_ptr -> nx_websocket_client_frame_opcode = 0;

    /* Check if using non-blocking mode.  */
    if (wait_option == 0)
    {
        return(NX_IN_PROGRESS);
    }

    while (1)
    {

        /* Receive response.  */
        status = _nx_websocket_client_packet_receive(client_ptr, &packet_ptr, wait_option);
        if (status)
        {
            return(status);
        }

        /* Process the response.  */
        status = _nx_websocket_client_connect_response_check(client_ptr, packet_ptr, wait_option);

        /* If status is NX_IN_PROGRESS, continue to receive connect response.
           Otherwise, break the while loop.  */
        if (status != NX_IN_PROGRESS)
        {
            break;
        }
    }

    /* Return status.  */
    return(status);
}

#ifdef NX_SECURE_ENABLE
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_secure_connect                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket secure connect.    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    tls_session                           Pointer to TLS session        */
/*    host                                  Pointer to host               */ 
/*    host_length                           Length of host                */
/*    uri_path                              Pointer to uri path           */ 
/*    uri_path_length                       Length of uri path            */
/*    protocol                              Pointer to protocol           */ 
/*    protocol_length                       Length of protocol            */
/*    bearer                                Pointer to bearer             */ 
/*    bearer_length                         Length of bearer              */
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_secure_connect   Actual websocket connect call */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_secure_connect(NX_WEBSOCKET_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session,
                                           UCHAR *host, UINT host_length,
                                           UCHAR *uri_path, UINT uri_path_length,
                                           UCHAR *protocol, UINT protocol_length,
                                           UCHAR *bearer, UINT bearer_length, UINT wait_option)
{

UINT        status;


    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID) || 
        (tls_session == NX_NULL) ||
        (host == NX_NULL) || (host_length == 0) || 
        (uri_path == NX_NULL) || (uri_path_length == 0))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual secure connect function.  */
    status = _nx_websocket_client_secure_connect(client_ptr, tls_session, host, host_length, uri_path, uri_path_length, protocol, protocol_length, bearer, bearer_length, wait_option);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_secure_connect                 PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function makes a WebSocket connection over TLS session to the  */
/*    server.                                                             */
/*    Note: Application must establish a TLS connection before.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    tls_session                           Pointer to TLS session        */
/*    host                                  Pointer to host               */ 
/*    host_length                           Length of host                */
/*    uri_path                              Pointer to uri path           */ 
/*    uri_path_length                       Length of uri path            */
/*    protocol                              Pointer to protocol           */ 
/*    protocol_length                       Length of protocol            */
/*    bearer                                Pointer to bearer             */ 
/*    bearer_length                         Length of bearer              */
/*    wait_option                           Wait option                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_connect_internal Make websocket connection     */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_secure_connect(NX_WEBSOCKET_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session,
                                          UCHAR *host, UINT host_length,
                                          UCHAR *uri_path, UINT uri_path_length,
                                          UCHAR *protocol, UINT protocol_length,
                                          UCHAR *bearer, UINT bearer_length, UINT wait_option)
{

UINT status;


    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Check the state.  */
    if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_CONNECTED)
    {

        /* Release the mutex and return */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_ALREADY_CONNECTED);
    }
    else if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_CONNECTING)
    {

        /* Release the mutex and return */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_CONNECTING);
    }
    else if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_INITIALIZE)
    {

        /* Release the mutex and return */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_INVALID_STATE);
    }

    /* Save the tls session pointer.  */
    client_ptr -> nx_websocket_client_tls_session_ptr = tls_session;
    client_ptr -> nx_websocket_client_use_tls = NX_TRUE;

    status = _nx_websocket_client_connect_internal(client_ptr, host, host_length, uri_path, uri_path_length, protocol, protocol_length, bearer, bearer_length, wait_option);

    /* Release the mutex and return */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
    return(status);
}
#endif /* NX_SECURE_ENABLE */

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_name_compare                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function compares two pieces of memory case insensitive.       */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    src                                   Pointer to source             */ 
/*    src_length                            Length of source              */ 
/*    dest                                  Pointer to destination        */ 
/*    dest_length                           Length of destination         */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_connect_response_process                       */
/*                                          Process connect response      */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_name_compare(UCHAR *src, ULONG src_length, UCHAR *dest, ULONG dest_length)
{
UCHAR   ch;

    /* Compare the length. */
    if((src_length != dest_length) ||
       (src == NX_NULL) || (dest == NX_NULL))
    {
        return(NX_WEBSOCKET_ERROR);
    }

    while(src_length)
    {

        /* Is src lowercase? */
        if((*src >= 'a') && (*src <= 'z'))
            ch = (UCHAR)(*src - 'a' + 'A');

        /* Is src uppercase? */
        else if((*src >= 'A') && (*src <= 'Z'))
            ch = (UCHAR)(*src - 'A' + 'a');
        else
            ch = *src;

        /* Compare case insensitive. */
        if((*src != *dest) && (ch != *dest))
        {
            return(NX_WEBSOCKET_ERROR);
        }

        /* Pickup next character. */
        src_length--;
        src++;
        dest++;
    }

    return(NX_WEBSOCKET_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_connect_response_process       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function processes connect response.                           */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_name_compare     Compare memory data           */
/*    _nx_sha1_initialize                   Initialize sha1               */
/*    _nx_sha1_update                       Update sha1                   */
/*    _nx_sha1_digest_calculate             Calculate sha1 digest         */
/*    _nx_utility_base64_encode             Base64 encode                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_connect_response_check                         */
/*                                          Check connect response        */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_connect_response_process(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET *packet_ptr)
{
UCHAR  *buffer_ptr;
UINT    offset;
UCHAR  *field_name;
UINT    field_name_length;
UCHAR  *field_value;
UINT    field_value_length;
UCHAR   digest[NX_WEBSOCKET_ACCEPT_DIGEST_SIZE];
UCHAR   key[NX_WEBSOCKET_ACCEPT_KEY_SIZE + 1];
UINT    key_size = 0;
UCHAR   upgrade_flag = NX_FALSE;
UCHAR   connection_flag = NX_FALSE;
UCHAR   accept_cnt = 0;

    NX_PARAMETER_NOT_USED(client_ptr);

    /* WebSocket opening handshake message format:
       HTTP/1.1 101 Switching Protocols
       Upgrade: websocket\r\n
       Server: Microsoft-HTTPAPI/2.0\r\n
       Sec-WebSocket-Protocol: mqtt\r\n
       Connection: Upgrade\r\n
       Sec-WebSocket-Accept: xxxxxxxxxx=\r\n
       Date: Mon, 06 Jun 2022 07:46:53 GMT\r\n
       \r\n
    */

    /* Setup pointer and offset.  */
    buffer_ptr = packet_ptr -> nx_packet_prepend_ptr;
    offset = 0;

    /* Check the length.  */
    if ((packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr) < 12)
    {
        return(NX_WEBSOCKET_INVALID_PACKET);
    }

    /* Check the status code. Must be "101" Switching Protocols.  */
    if ((buffer_ptr[9] != '1') || (buffer_ptr[10] != '0') || (buffer_ptr[11] != '1'))
    {
        return(NX_WEBSOCKET_INVALID_STATUS_CODE);
    }

    /* Skip over the first HTTP line (HTTP/1.1 101 Switching Protocols\r\n).  */
    offset = 12;
    buffer_ptr += 12;
    while(((buffer_ptr + 1) < packet_ptr -> nx_packet_append_ptr) &&
          (*buffer_ptr != '\r') && (*(buffer_ptr + 1) != '\n'))
    {
        buffer_ptr++;
        offset++;
    }

    /* Skip over the CR,LF. */
    buffer_ptr += 2;
    offset += 2;

    /* Loop until we find the "cr,lf,cr,lf" token.  */
    while (((buffer_ptr + 1) < packet_ptr -> nx_packet_append_ptr) && (*buffer_ptr != 0))
    {

        /* Check for the <cr,lf,cr,lf> token.  This signals a blank line, which also
           specifies the start of the content.  */
        if ((*buffer_ptr == '\r') &&
            (*(buffer_ptr + 1) ==  '\n'))
        {

            /* Adjust the offset.  */
            offset = offset + 2;
            break;
        }

        /* We haven't seen the <cr,lf,cr,lf> so we are still processing header data.
           Extract the field name and it's value.  */
        field_name = buffer_ptr;
        field_name_length = 0;

        /* Look for the ':' that separates the field name from its value. */
        while ((buffer_ptr < packet_ptr -> nx_packet_append_ptr) && (*buffer_ptr != ':'))
        {
            buffer_ptr++;
            field_name_length++;
        }
        offset += field_name_length;

        /* Skip ':'.  */
        buffer_ptr++;
        offset++;

        /* Now skip over white space. */
        while ((buffer_ptr < packet_ptr -> nx_packet_append_ptr) && (*buffer_ptr == ' '))
        {
            buffer_ptr++;
            offset++;
        }

        /* Now get the field value. */
        field_value = buffer_ptr;
        field_value_length = 0;

        /* Loop until we see a <CR, LF>. */
        while (((buffer_ptr + 1) < packet_ptr -> nx_packet_append_ptr) && (*buffer_ptr != '\r') && (*(buffer_ptr+1) != '\n'))
        {
            buffer_ptr++;
            field_value_length++;
        }
        offset += field_value_length;

        /* Skip over the CR,LF. */
        buffer_ptr += 2;
        offset += 2;

        /* Check the upgrade.  */
        if (_nx_websocket_client_name_compare((UCHAR *)field_name, field_name_length, (UCHAR *)"Upgrade", sizeof("Upgrade") - 1) == NX_SUCCESS)
        {
            if (_nx_websocket_client_name_compare((UCHAR *)field_value, field_value_length, (UCHAR *)"websocket", sizeof("websocket") - 1))
            {
                return(NX_WEBSOCKET_INVALID_PACKET);
            }

            upgrade_flag = NX_TRUE;
        }
        else if (_nx_websocket_client_name_compare((UCHAR *)field_name, field_name_length, (UCHAR *)"Connection", sizeof("Connection") - 1) == NX_SUCCESS)
        {
            if (_nx_websocket_client_name_compare((UCHAR *)field_value, field_value_length, (UCHAR *)"Upgrade", sizeof("Upgrade") - 1))
            {
                return(NX_WEBSOCKET_INVALID_PACKET);
            }

            connection_flag = NX_TRUE;
        }
        else if (_nx_websocket_client_name_compare((UCHAR *)field_name, field_name_length, (UCHAR *)"Sec-WebSocket-Protocol", sizeof("Sec-WebSocket-Protocol") - 1) == NX_SUCCESS)
        {
            if (_nx_websocket_client_name_compare((UCHAR *)field_value, field_value_length, client_ptr -> nx_websocket_client_subprotocol, client_ptr -> nx_websocket_client_subprotocol_length))
            {
                return(NX_WEBSOCKET_INVALID_PACKET);
            }
        }
        else if (_nx_websocket_client_name_compare((UCHAR *)field_name, field_name_length, (UCHAR *)"Sec-WebSocket-Accept", sizeof("Sec-WebSocket-Accept") - 1) == NX_SUCCESS)
        {

            /* Calculate the SHA-1 hash of the concatenation of the client key and the Globally Unique Identifier (GUID)
               Referenced in RFC 6455, Section 1.3, Page 6 */
            _nx_sha1_initialize(&(client_ptr -> nx_websocket_client_sha1));
            _nx_sha1_update(&(client_ptr -> nx_websocket_client_sha1), client_ptr->nx_websocket_client_key, client_ptr->nx_websocket_client_key_size);
            _nx_sha1_update(&(client_ptr -> nx_websocket_client_sha1), (UCHAR*)NX_WEBSOCKET_ACCEPT_PREDEFINED_GUID, NX_WEBSOCKET_ACCEPT_PREDEFINED_GUID_SIZE);
            _nx_sha1_digest_calculate(&(client_ptr -> nx_websocket_client_sha1), digest);

            /* Encode the hash and compare it with the field value from the server.  */
            _nx_utility_base64_encode(digest, NX_WEBSOCKET_ACCEPT_DIGEST_SIZE, key, (NX_WEBSOCKET_ACCEPT_KEY_SIZE + 1), &key_size);
            if ((field_value_length != NX_WEBSOCKET_ACCEPT_KEY_SIZE) || (memcmp((void *)field_value, (void *)key, NX_WEBSOCKET_ACCEPT_KEY_SIZE))) /* Use case of memcpy is verified. */
            {
                return(NX_WEBSOCKET_INVALID_PACKET);
            }

            accept_cnt++;
        }
    }

    /* Check if the all fields are processed and found as required.  */
    if ((offset != packet_ptr -> nx_packet_length) ||
        (upgrade_flag != NX_TRUE) || (connection_flag != NX_TRUE) ||
        (accept_cnt != 1)) /* Sec-WebSocket-Accept field is allowed occur once only.
                              Reference in RFC 6455, Section 11.3.3 and 11.3.4, Page 59-60 */
    {
        return(NX_WEBSOCKET_INVALID_PACKET);
    }

    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_disconnect                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket disconnect.        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_disconnect       Actual disconnect call        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_disconnect(NX_WEBSOCKET_CLIENT *client_ptr, UINT wait_option)
{

UINT        status;


    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual disconnect function.  */
    status = _nx_websocket_client_disconnect(client_ptr, wait_option);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_disconnect                     PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function disconnects the WebSocket connection created          */
/*    previously.                                                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    wait_option                           Wait option                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_packet_allocate  Allocate websocket packet     */
/*    _nx_websocket_client_send             Send websocket packet         */
/*    nx_packet_release                     Release websocket packet      */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_disconnect(NX_WEBSOCKET_CLIENT *client_ptr, UINT wait_option)
{

UINT        status;
NX_PACKET  *packet_ptr;


    /* Check the state.  */
    if ((client_ptr -> nx_websocket_client_state <= NX_WEBSOCKET_CLIENT_STATE_IDLE) || (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_SENT))
    {

        /* Return error status */
        return(NX_WEBSOCKET_INVALID_STATE);
    }
    else if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_CONNECTING)
    {

        /* Update the state and return */
        client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_IDLE;
        return(NX_WEBSOCKET_NOT_CONNECTED);
    }

    /* Allocate a packet.  */
    status = _nx_websocket_client_packet_allocate(client_ptr, &packet_ptr, wait_option);

    /* Check the status.  */
    if (status == NX_SUCCESS)
    {

        /* Send out the packet.  */
        status = _nx_websocket_client_send(client_ptr, packet_ptr, NX_WEBSOCKET_OPCODE_CONNECTION_CLOSE, NX_TRUE, wait_option);

        /* Check the status.  */
        if (status)
        {
            nx_packet_release(packet_ptr);
        }
    }

    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Check if the CLOSE frame has already been received before */
    if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_RECEIVED)
    {
        client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_IDLE;
    }
    else /* i.e. The state is CONNECTED */
    {
        client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_SENT;
    }

    /* Release the mutex and return completion status */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_send                          PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket send.              */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet             */ 
/*    code                                  Opcode: text or binary frame  */ 
/*    is_final                              Flag: final fragment or not   */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_send             Actual websocket send call    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_send(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET *packet_ptr, UINT code, UINT is_final, UINT wait_option)
{

UINT        status;


    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID) || 
        (packet_ptr == NX_NULL))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual send function.  */
    status = _nx_websocket_client_send(client_ptr, packet_ptr, code, is_final, wait_option);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_send                           PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends Websocket data frame to server.                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet             */ 
/*    code                                  Opcode: text or binary frame  */ 
/*    is_final                              Flag: final fragment or not   */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_packet_send      Send websocket packet         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_send(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET *packet_ptr, UINT code, UINT is_final, UINT wait_option)
{

UINT status;
UCHAR *data_ptr;
NX_PACKET *data_packet;
USHORT message;
ULONG tmp;
UCHAR masking_key[4];
UINT mask_id = 0;
UINT header_size = NX_WEBSOCKET_HEADER_NORMAL_SIZE;


    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Check the state */
    if (client_ptr -> nx_websocket_client_state < NX_WEBSOCKET_CLIENT_STATE_CONNECTED)
    {

        /* Release the mutex and return error status */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_NOT_CONNECTED);
    }
    else if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_SENT)
    {

        /* Release the mutex and return error status */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_INVALID_STATE);
    }

    /* Check the payload length.  */
    if (packet_ptr -> nx_packet_length > 65535)
    {

        /* Release the mutex and return error status */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_NOT_SUPPORTED);
    }
    else if (packet_ptr -> nx_packet_length > 125)
    {
        header_size += 2;
    }

    /* Check the packet.  */
    if ((UINT)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start) < header_size)
    {

        /* Release the mutex and return error status */
        tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
        return(NX_WEBSOCKET_INVALID_PACKET);
    }

    /* Adjust the pointer for filling the WebSocket header.  */
    data_ptr = packet_ptr -> nx_packet_prepend_ptr - header_size;

    /* Fill the first byte (FIN + OPCODE) in header.  */
    if (is_final == NX_TRUE)
    {
        *data_ptr = (UCHAR)(NX_WEBSOCKET_FIN | code);
    }
    else
    {
        *data_ptr = (UCHAR)code;
    }
    data_ptr++;

    /* Fill the next byte for MASK and Payload length.  */
    *data_ptr = NX_WEBSOCKET_MASK;
    if (packet_ptr -> nx_packet_length < 125)
    {
        *data_ptr |= (UCHAR)packet_ptr -> nx_packet_length;
        data_ptr++;
    }
    else
    {
        *data_ptr |= NX_WEBSOCKET_PAYLOAD_LEN_16BITS;
        data_ptr++;

        /* Fill the next two bytes for extended payload length.  */
        message = (USHORT)packet_ptr -> nx_packet_length;
        NX_CHANGE_USHORT_ENDIAN(message);
        memcpy(data_ptr, &message, NX_WEBSOCKET_EXTENDED_PAYLOAD_16BITS_SIZE); /* Use case of memcpy is verified. */
        data_ptr += NX_WEBSOCKET_EXTENDED_PAYLOAD_16BITS_SIZE;
    }

    /* Fill the masking key, the masking key is a 32-bit value chosen at random, must mask websocket data from client*/
    tmp = (ULONG)NX_RAND();
    masking_key[0] = (UCHAR)(tmp >> 24);
    masking_key[1] = (UCHAR)(tmp >> 16);
    masking_key[2] = (UCHAR)(tmp >> 8);
    masking_key[3] = (UCHAR)(tmp);
    memcpy(data_ptr, &masking_key, NX_WEBSOCKET_MASKING_KEY_SIZE); /* Use case of memcpy is verified. */
    data_ptr += NX_WEBSOCKET_MASKING_KEY_SIZE;

    /* Mask all payload data.  */
    data_packet = packet_ptr;
#ifndef NX_DISABLE_PACKET_CHAIN
    while(data_packet)
    {
#endif /* NX_DISABLE_PACKET_CHAIN  */

        data_ptr = data_packet -> nx_packet_prepend_ptr;
        while(data_ptr < data_packet -> nx_packet_append_ptr)
        {
            *data_ptr ^= masking_key[mask_id % 4];
            mask_id++;
            data_ptr++;
        }

#ifndef NX_DISABLE_PACKET_CHAIN
        data_packet = data_packet -> nx_packet_next;
    }
#endif /* NX_DISABLE_PACKET_CHAIN  */

    /* Update prepend pointer and packet length to include WebSocket header.  */
    packet_ptr -> nx_packet_prepend_ptr -= header_size;
    packet_ptr -> nx_packet_length += header_size;

    /* Release the mutex */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));

    /* Send out the packet.  */
    status = _nx_websocket_client_packet_send(client_ptr, packet_ptr, wait_option);

    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_receive                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket receive.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    code                                  Opcode: text or binary frame  */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_receive          Actual websocket receive call */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_receive(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, UINT *code, UINT wait_option)
{

UINT        status;


    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID) ||
        (packet_ptr == NX_NULL) || (code == NX_NULL))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual process function.  */
    status = _nx_websocket_client_receive(client_ptr, packet_ptr, code, wait_option);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_receive                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function receives Websocket data frame from server.            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    code                                  Opcode: text or binary frame  */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_packet_receive   Receive websocket packet      */
/*    _nx_websocket_client_data_process     Process data frame            */
/*    _nx_websocket_client_connect_response_check                         */
/*                                          Check connect response        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_receive(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, UINT *code, UINT wait_option)
{

UINT        status = NX_SUCCESS;


    /* Initialize here since packet_ptr will be compared with NX_NULL in following function */
    *packet_ptr = NX_NULL;

    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    while (1) /* The while loop ensures parsing all received packets. */
    {

        /* Check the state */
        if ((client_ptr -> nx_websocket_client_state < NX_WEBSOCKET_CLIENT_STATE_CONNECTING) ||
            (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_RECEIVED))
        {

            /* Release the mutex and return error status */
            tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
            return(NX_WEBSOCKET_INVALID_STATE);
        }

        /* If the state is NX_WEBSOCKET_CLIENT_STATE_CONNECTING, the received packet should be connect response.  */
        if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_CONNECTING)
        {

            /* Receive the data packet */
            status = _nx_websocket_client_packet_receive(client_ptr, packet_ptr, wait_option);
            if (status != NX_SUCCESS)
            {

                /* Release the mutex and return status */
                tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
                return(status);
            }

            /* Process the connect response.  */
            status = _nx_websocket_client_connect_response_check(client_ptr, *packet_ptr, wait_option);

            if ((status == NX_SUCCESS) || (status == NX_IN_PROGRESS))
            {

                /* Continue to receive remaining connect response or application data.  */
                continue;
            }
            else
            {

                /* Release the mutex and return status */
                tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
                return(status);
            }
        }
        else
        {

            /* Check if there is an existing complete frame in the waiting list */
            if ((status == NX_SUCCESS) && (client_ptr -> nx_websocket_client_processing_packet != NX_NULL))
            {

                /* Parse the data packet. */
                status = _nx_websocket_client_data_process(client_ptr, packet_ptr, code);
                if (status != NX_CONTINUE)
                {

                    /* Release the mutex and return directly if a complete frame is parsed or any error status is found. */
                    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
                    return(status);
                }
            }

            /* No existing frame, go to receive the data packet */
            status = _nx_websocket_client_packet_receive(client_ptr, packet_ptr, wait_option);
            if (status != NX_SUCCESS)
            {

                /* Release the mutex and return status */
                tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
                return(status);
            }

            /* Parse with the new data packet. */
            status = _nx_websocket_client_data_process(client_ptr, packet_ptr, code);
            if (status != NX_CONTINUE)
            {

                /* Release the mutex and return directly if a complete frame is parsed or any error status is found. */
                tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));
                return(status);
            }

            /* Due to no application data found, continue to check if there is any pending data packet. */
        }
    }
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_data_process                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function processes Websocket data frame from server.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    code                                  Opcode: text or binary frame  */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_packet_data_extract_offset         Extract data from packet      */
/*    nx_packet_allocate                    Allocate a packet             */
/*    _nx_websocket_client_packet_trim      Trim data from packet         */
/*    _nx_websocket_client_cleanup          Cleanup resource              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_receive          Receive websocket data        */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_data_process(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, UINT *code)
{
UINT  status;
UCHAR fin_bit = NX_FALSE;
UCHAR opcode = 0;
UCHAR bytes[4];
ULONG payload_length;
ULONG offset = 0;
ULONG bytes_copied;
ULONG packet_length;
NX_PACKET *data_packet;
UCHAR *data_ptr;


    /* Is there a packet waiting for processing? */
    if (client_ptr -> nx_websocket_client_processing_packet)
    {
        if (*packet_ptr != NX_NULL)
        {

            /* Yes. Link received packet to existing one. */
            if (client_ptr -> nx_websocket_client_processing_packet -> nx_packet_last)
            {
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_last -> nx_packet_next = *packet_ptr;
            }
            else
            {
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_next = *packet_ptr;
            }
            if ((*packet_ptr) -> nx_packet_last)
            {
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_last = (*packet_ptr) -> nx_packet_last;
            }
            else
            {
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_last = *packet_ptr;
            }
            client_ptr -> nx_websocket_client_processing_packet -> nx_packet_length += (*packet_ptr) -> nx_packet_length;
        }

        /* Make packet_ptr point to the waiting list */
        *packet_ptr = client_ptr -> nx_websocket_client_processing_packet;
    }
    else
    {
        if (*packet_ptr == NX_NULL)
        {
            return(NX_WEBSOCKET_INVALID_PACKET);
        }

        client_ptr -> nx_websocket_client_processing_packet = *packet_ptr;
    }

    /* Check if the websocket frame header shall be parsed and found first */
    if (client_ptr -> nx_websocket_client_frame_header_found == NX_FALSE)
    {

        /* Parse the first 2 bytes. */
        if (nx_packet_data_extract_offset(*packet_ptr, offset, bytes, NX_WEBSOCKET_HEADER_MINIMUM_LENGTH, &bytes_copied)
            || (bytes_copied < NX_WEBSOCKET_HEADER_MINIMUM_LENGTH))
        {
            return(NX_CONTINUE);
        }

        /* Update the offset */
        offset += NX_WEBSOCKET_HEADER_MINIMUM_LENGTH;

        /* Obtain the fin bit and opcode */
        fin_bit = bytes[0] & NX_WEBSOCKET_FIN_MASK;
        opcode = bytes[0] & NX_WEBSOCKET_OPCODE_MASK;

        /* Parse the mask bit and payload length */
        if (bytes[1] & NX_WEBSOCKET_MASK)
        {
            client_ptr -> nx_websocket_client_frame_masked = NX_TRUE;
        }
        else
        {
            client_ptr -> nx_websocket_client_frame_masked = NX_FALSE;
        }

        payload_length = (UCHAR)(bytes[1] & NX_WEBSOCKET_PAYLOAD_LEN_MASK);
        if (payload_length < 126)
        {

            /* No extend payload length; record data payload length directly. */
        }
        else if (payload_length == 126)
        {

            /* Extract the 16-bit extended data payload length */
            if (nx_packet_data_extract_offset(*packet_ptr, offset, bytes, NX_WEBSOCKET_EXTENDED_PAYLOAD_16BITS_SIZE, &bytes_copied)
                || (bytes_copied < NX_WEBSOCKET_EXTENDED_PAYLOAD_16BITS_SIZE))
            {
                return(NX_CONTINUE);
            }

            /* Record 16-bit data payload length. */
            payload_length = ((((ULONG)bytes[0]) << 8) + (ULONG)bytes[1]);

            /* Add the byte count by the payload size */
            offset += NX_WEBSOCKET_EXTENDED_PAYLOAD_16BITS_SIZE;
        }
        else
        {

            /* Since 64 bits extend payload length is not supported, clean up and return directly. */
            _nx_websocket_client_cleanup(client_ptr);
            return(NX_NOT_SUPPORTED);
        }

        /* Parse the masking key */
        if (client_ptr -> nx_websocket_client_frame_masked == NX_TRUE)
        {
            if (nx_packet_data_extract_offset(*packet_ptr, offset, bytes, NX_WEBSOCKET_MASKING_KEY_SIZE, &bytes_copied)
                || (bytes_copied < NX_WEBSOCKET_MASKING_KEY_SIZE))
            {
                return(NX_CONTINUE);
            }

            /* Get the masking key. */
            memcpy(client_ptr -> nx_websocket_client_frame_masking_key, bytes, NX_WEBSOCKET_MASKING_KEY_SIZE); /* Use case of memcpy is verified. */

            /* Add the byte count by the masking key size */
            offset += NX_WEBSOCKET_MASKING_KEY_SIZE;
        }

        /* Set the flag to indicate the frame header found, and re-initialize corresponding variables */
        client_ptr -> nx_websocket_client_frame_header_found = NX_TRUE;
        client_ptr -> nx_websocket_client_frame_data_received = 0;

        /* Record the payload length for judging if all payload data received */
        client_ptr -> nx_websocket_client_frame_data_length = payload_length;

        /* Check the rules apply to fragmentation corresponding to the FIN bit and opcode
        RFC 6455, Section 5.4, Page 33-35 */
        if (fin_bit == NX_WEBSOCKET_FIN) /* This is the final frame (tip: the first frame may also be the final frame) */
        {
            if (client_ptr -> nx_websocket_client_frame_fragmented == NX_FALSE) /* A single unfragmented frame shall be received */
            {
                if (opcode == 0) /* The opcode should not denotes a continuation frame for a single unfragmented frame */
                {
                    _nx_websocket_client_cleanup(client_ptr);
                    return(NX_INVALID_PACKET);
                }

                /* Update the header opcode */
                client_ptr -> nx_websocket_client_frame_opcode = opcode;
            }
            else /* This is the termination frame in overall fragmented frames */
            {
                if (opcode != 0) /* The opcode of the termination frame shall be zero */
                {
                    _nx_websocket_client_cleanup(client_ptr);
                    return(NX_INVALID_PACKET);
                }

                /* Set the header flag to be unfragmented for next time to use */
                client_ptr -> nx_websocket_client_frame_fragmented = NX_FALSE;
            }
        }
        else /* This is not the final header */
        {
            if (client_ptr -> nx_websocket_client_frame_fragmented == NX_FALSE) /* This is the beginning frame in fragmented frames */
            {

                /* The opcode of the beginning frame shall indicate the opcode of overall fragmented frames. Besides,
                since control frames cannot be fragmented, the supported frame type shall be text or binary */
                if ((opcode != NX_WEBSOCKET_OPCODE_BINARY_FRAME) && (opcode != NX_WEBSOCKET_OPCODE_TEXT_FRAME))
                {
                    _nx_websocket_client_cleanup(client_ptr);
                    return(NX_INVALID_PACKET);
                }

                /* Update the frame fragmented flag and the opcode since a beginning frame is received */
                client_ptr -> nx_websocket_client_frame_fragmented = NX_TRUE;
                client_ptr -> nx_websocket_client_frame_opcode = opcode;
            }
            else /* This is a continuation frame in overall fragmented frames */
            {
                if (opcode != 0) /* The opcode of a continuation frame shall be zero */
                {
                    _nx_websocket_client_cleanup(client_ptr);
                    return(NX_INVALID_PACKET);
                }
            }
        }

        /* Trim the WebSocket header */
        status = _nx_websocket_client_packet_trim(client_ptr, packet_ptr, offset);
        client_ptr -> nx_websocket_client_processing_packet = *packet_ptr;
        if (status)
        {
            if (status == NX_NO_PACKET)
            {

                /* Try to receive more payload data from TCP/TLS if the packet holds the WebSocket header only */
                return(NX_CONTINUE);
            }

            /* Return error status */
            return(status);
        }
    }

    /* Reset payload length and use the variable to count the data length processed by the function call this time */
    payload_length = 0;

    /* Unmask payload data if there is masking key. */
    if (client_ptr -> nx_websocket_client_frame_masked == NX_TRUE)
    {
        data_packet = (*packet_ptr);
#ifndef NX_DISABLE_PACKET_CHAIN
        while (data_packet && client_ptr -> nx_websocket_client_frame_header_found)
        {
#endif /* NX_DISABLE_PACKET_CHAIN  */

            data_ptr = (*packet_ptr) -> nx_packet_prepend_ptr;
            while (data_ptr < data_packet -> nx_packet_append_ptr)
            {

                /* Unmask payload data byte by byte */
                *data_ptr ^= client_ptr -> nx_websocket_client_frame_masking_key[client_ptr -> nx_websocket_client_frame_data_received % 4];
                data_ptr++;

                /* Increase the payload length for the usage in frame process */
                payload_length++;

                /* Check and jump out if all data payload in the frame have been processed. */
                client_ptr -> nx_websocket_client_frame_data_received++;
                if (client_ptr -> nx_websocket_client_frame_data_received >= client_ptr -> nx_websocket_client_frame_data_length)
                {

                    /* Reset the frame header flag as not found and break. */
                    client_ptr -> nx_websocket_client_frame_header_found = NX_FALSE;
                    break;
                }
            }

#ifndef NX_DISABLE_PACKET_CHAIN
            data_packet = data_packet -> nx_packet_next;
        }
#endif /* NX_DISABLE_PACKET_CHAIN  */
    }

    /* Add the payload length if no masking key */
    else
    {

        /* Check and adjust received data length for processing */
        payload_length = (*packet_ptr) -> nx_packet_length;
        if (payload_length >= (client_ptr -> nx_websocket_client_frame_data_length - client_ptr -> nx_websocket_client_frame_data_received))
        {

            /* The maximum the payload length for each frame process shall not exceed the remaining frame data to be received */
            payload_length = (client_ptr -> nx_websocket_client_frame_data_length - client_ptr -> nx_websocket_client_frame_data_received);

            /* Reset the frame header flag as not found. */
            client_ptr -> nx_websocket_client_frame_header_found = NX_FALSE;
        }

        /* Add the length to the total count */
        client_ptr -> nx_websocket_client_frame_data_received += payload_length;
    }

    /* Check the opcode for the received frame, and return corresponding status. */
    switch (opcode)
    {
        case NX_WEBSOCKET_OPCODE_CONTINUATION_FRAME:
        case NX_WEBSOCKET_OPCODE_TEXT_FRAME:
        case NX_WEBSOCKET_OPCODE_BINARY_FRAME:
        {

            /* Assign the return opcode by the pre-stored opcode */
            *code = client_ptr -> nx_websocket_client_frame_opcode;

            /* Update the offset by payload length */
            offset = payload_length;

            /* For a data frame (i.e. text/binary frame), search and find the end of the complete frame */
            data_packet = *packet_ptr;
            packet_length = (ULONG)(data_packet -> nx_packet_append_ptr - data_packet -> nx_packet_prepend_ptr);
            while (packet_length < offset)
            {
                offset -= packet_length;

                /* Move the current data packet pointer to next and compute the length of next packet */
                data_packet = data_packet -> nx_packet_next;
                packet_length = (ULONG)(data_packet -> nx_packet_append_ptr - data_packet -> nx_packet_prepend_ptr);
            }

            /* After subtracting by the offset, packet_length represents the size of remaining data in the single packet to be linked into the waiting list */
            packet_length -= offset;

            /* Check if the frame end is just in the end of one of the packet(s) */
            if (packet_length == 0)
            {

                /* Put remaining packet(s) into the waiting list */
                client_ptr -> nx_websocket_client_processing_packet = data_packet -> nx_packet_next;

                /* Check if there is data remaining and determine whether to go on to following logic. */
                if (data_packet -> nx_packet_next == NX_NULL)
                {

                    /* The packet is fully parsed, return success directly. */
                    return(NX_WEBSOCKET_SUCCESS);
                }
            }
            else /* One more frame in the packet */
            {

                /* Release the mutex. */
                tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));

                /* Allocate a packet for the remaining data in the packet. */
                status = nx_packet_allocate((*packet_ptr) -> nx_packet_pool_owner, &client_ptr -> nx_websocket_client_processing_packet, NX_RECEIVE_PACKET, NX_WAIT_FOREVER);

                /* The return status may be not NX_SUCCESS only there is an unexpected issue from the packet pool (e.g. a delete operation ).
                   Assert here for the unexpected issue shall not happen in normal status */
                NX_ASSERT(status == NX_SUCCESS);

                /* Obtain the mutex again. */
                tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

                /* Copy the contents of the remaining part of current packet into the new allocated packet */
                memcpy((void *)client_ptr -> nx_websocket_client_processing_packet -> nx_packet_prepend_ptr,
                       (void *)(data_packet -> nx_packet_prepend_ptr + offset), packet_length); /* Use case of memcpy is verified. */

                /* Move the append pointer with by the extended length */
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_append_ptr += packet_length;
                
                /* Link the possibly had remaining packet(s) to the waiting list. The overall packet length will be updated outside this else branch */
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_next = data_packet -> nx_packet_next;
            }

            /* Update the last packet pointer in the waiting liast */
            if (client_ptr -> nx_websocket_client_processing_packet -> nx_packet_next)
            {
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_last = (*packet_ptr) -> nx_packet_last;
            }
            else
            {
                client_ptr -> nx_websocket_client_processing_packet -> nx_packet_last = NX_NULL;
            }

            /* Update the overall packet length for the waiting list */
            client_ptr -> nx_websocket_client_processing_packet -> nx_packet_length = (*packet_ptr) -> nx_packet_length - payload_length;

            /* Disconnect the link between the return packet and the waiting list */
            data_packet -> nx_packet_next = NX_NULL;
            data_packet -> nx_packet_append_ptr -= packet_length;
            (*packet_ptr) -> nx_packet_length = payload_length;

            /* Update the last packet pointer of the returned packet */
            if ((*packet_ptr) -> nx_packet_next)
            {
                (*packet_ptr) -> nx_packet_last = data_packet;
            }
            else
            {
                (*packet_ptr) -> nx_packet_last = NX_NULL;
            }

            /* Return success status */
            return(NX_WEBSOCKET_SUCCESS);
        }

        case NX_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
        {
            /* Make sure the complete control frame is received */
            if (client_ptr -> nx_websocket_client_frame_data_received < client_ptr -> nx_websocket_client_frame_data_length)
            {
                return(NX_CONTINUE);
            }

            /* A disconnection is informed, notify the application.  */
            if (client_ptr -> nx_websocket_client_connection_status_callback)
            {
                client_ptr -> nx_websocket_client_connection_status_callback(client_ptr, client_ptr -> nx_websocket_client_connection_context, NX_WEBSOCKET_DISCONNECTED);
            }

            /* Check the current state and update the state when the CLOSE frame is found */
            if (client_ptr -> nx_websocket_client_state == NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_SENT)
            {
                client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_IDLE;
            }
            else
            {
                client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_DISCONNECT_RECEIVED;
            }

            /* Return disconnect received status only, without returning any packet data, since it is required no more data after the Close frame.
               There is no need to release the packet_ptr since it points to the same memory region as the waiting list
               Referenced in RFC 6455, Section 5.5.1, Page 36 */
            _nx_websocket_client_cleanup(client_ptr);
            return(NX_WEBSOCKET_DISCONNECTED);
        }

        case NX_WEBSOCKET_OPCODE_PING:
        case NX_WEBSOCKET_OPCODE_PONG:
        {

            /* Make sure the complete control frame is received */
            if (client_ptr -> nx_websocket_client_frame_data_received < client_ptr -> nx_websocket_client_frame_data_length)
            {
                return(NX_CONTINUE);
            }

            /* Trim payload data in the frame. */
            status = _nx_websocket_client_packet_trim(client_ptr, packet_ptr, client_ptr -> nx_websocket_client_frame_data_received);

            /* Update the waiting list */
            client_ptr -> nx_websocket_client_processing_packet = *packet_ptr;

            /* Check if error status happens */
            if ((status != NX_WEBSOCKET_SUCCESS) && (status != NX_NO_PACKET))
            {
                return(status);
            }

            /* A PING/PONG frame is parsed and found, continue to check any more data or frame received */
            return(NX_CONTINUE);
        }

        default:
        {

            /* Clean up and return invalid status */
            _nx_websocket_client_cleanup(client_ptr);
            return(NX_INVALID_PACKET);
        }
    }
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_packet_trim                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function trims extra bytes from the data packet.               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    trim_size                             Number of bytes to remove     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_packet_release                     Release the packet            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_data_process     Process data frame            */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_packet_trim(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG trim_size)
{

ULONG       packet_length;
NX_PACKET   *head_packet_ptr = *packet_ptr;
NX_PACKET   *previous_packet_ptr = NX_NULL;

    NX_PARAMETER_NOT_USED(client_ptr);

    /* The trim size shall be less than or equal to the packet length */
    if ((*packet_ptr) -> nx_packet_length < trim_size)
    {
        return(NX_PACKET_OFFSET_ERROR);
    }
    packet_length = (*packet_ptr) -> nx_packet_length - trim_size;

    /* Search and find the trim point */
    while ((ULONG)((*packet_ptr) -> nx_packet_append_ptr - (*packet_ptr) -> nx_packet_prepend_ptr) <= trim_size)
    {
        trim_size -= (ULONG)((*packet_ptr) -> nx_packet_append_ptr - (*packet_ptr) -> nx_packet_prepend_ptr);

        previous_packet_ptr = *packet_ptr;
        *packet_ptr = (*packet_ptr) -> nx_packet_next;

        if (*packet_ptr == NX_NULL)
        {
            break;
        }
    }

    /* Disconnect the link if the trim point is not in the head packet */
    if (previous_packet_ptr != NX_NULL)
    {
        previous_packet_ptr -> nx_packet_next = NX_NULL;

        /* Set the nx_packet_last pointer due to the packet header changed */
        if (*packet_ptr)
        {
            (*packet_ptr) -> nx_packet_last = head_packet_ptr -> nx_packet_last;
        }

        nx_packet_release(head_packet_ptr);
    }

    if (*packet_ptr)
    {

        /* Adjust current packet */
        (*packet_ptr) -> nx_packet_prepend_ptr += trim_size;
        (*packet_ptr) -> nx_packet_length = packet_length;

        return(NX_WEBSOCKET_SUCCESS);
    }

    /* Return this value to tell the caller that the whole packet is trimmed */
    return(NX_NO_PACKET);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_packet_allocate               PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket packet allocate.   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_packet_allocate  Actual websocket allocate call*/
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_packet_allocate(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option)
{

UINT        status;


    /* Check for invalid input pointers */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID) || (packet_ptr == NX_NULL))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual process function.  */
    status = _nx_websocket_client_packet_allocate(client_ptr, packet_ptr, wait_option);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_packet_allocate                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function allocates a Websocket packet.                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_packet_allocate                    Allocate a packet             */
/*    nx_packet_release                     Release packet                */
/*    nx_secure_tls_packet_allocate         Allocate a TLS packet         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_packet_allocate(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option)
{

UINT        status;


#ifdef NX_SECURE_ENABLE
    if (client_ptr -> nx_websocket_client_use_tls)
    {

        /* Use TLS packet allocate.  The TLS packet allocate is able to count for
           TLS-related header space including crypto initial vector area. */
        status = nx_secure_tls_packet_allocate(client_ptr -> nx_websocket_client_tls_session_ptr,
                                               client_ptr -> nx_websocket_client_packet_pool_ptr,
                                               packet_ptr, TX_WAIT_FOREVER);
    }
    else
    {
#endif /* NX_SECURE_ENABLE */

        /* Allocate packet.  */
        if (client_ptr -> nx_websocket_client_socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
        {
            status =  nx_packet_allocate(client_ptr -> nx_websocket_client_packet_pool_ptr,
                                         packet_ptr,
                                         NX_IPv4_TCP_PACKET, wait_option);
        }
        else
        {
            status =  nx_packet_allocate(client_ptr -> nx_websocket_client_packet_pool_ptr,
                                         packet_ptr,
                                         NX_IPv6_TCP_PACKET, wait_option);
        }
#ifdef NX_SECURE_ENABLE
    }
#endif /* NX_SECURE_ENABLE */

    if (status == NX_SUCCESS)
    {

        /* Check the buffer size for the basic data header of websocket.  */
        if (((ULONG)(((*packet_ptr) -> nx_packet_data_end) - ((*packet_ptr) -> nx_packet_prepend_ptr))) < NX_WEBSOCKET_HEADER_SIZE)
        {

            /* Packet buffer is too small. */
            nx_packet_release(*packet_ptr);
            return(NX_WEBSOCKET_INVALID_PACKET);
        }

        /* Adjust the pointers.  */
        ((*packet_ptr) -> nx_packet_prepend_ptr) += NX_WEBSOCKET_HEADER_SIZE;
        ((*packet_ptr) -> nx_packet_append_ptr) += NX_WEBSOCKET_HEADER_SIZE;
    }

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_packet_send                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends Websocket packet.                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet             */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_tcp_socket_send                    Send tcp packet               */
/*    nx_secure_tls_session_send            Send tls packet               */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_connect_internal Make websocket connection     */
/*    _nx_websocket_client_send             Send websocket data frame     */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_packet_send(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET *packet_ptr, ULONG wait_option)
{

UINT status;

#ifdef NX_SECURE_ENABLE
    if (client_ptr -> nx_websocket_client_use_tls)
    {
        status = nx_secure_tls_session_send(client_ptr -> nx_websocket_client_tls_session_ptr, packet_ptr, wait_option);
    }
    else
    {
        status = nx_tcp_socket_send(client_ptr -> nx_websocket_client_socket_ptr, packet_ptr, wait_option);
    }
#else
    status = nx_tcp_socket_send(client_ptr -> nx_websocket_client_socket_ptr, packet_ptr, wait_option);
#endif /* NX_SECURE_ENABLE */

    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_packet_receive                 PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function receives Websocket packet.                            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet pointer     */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_tcp_socket_receive                 Receive tcp packet            */
/*    nx_secure_tls_session_receive         Receive tls packet            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_connect_internal Make websocket connection     */
/*    _nx_websocket_client_receive          Receive websocket data frame  */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_packet_receive(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option)
{

UINT status;


    /* Release the mutex first */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));

#ifdef NX_SECURE_ENABLE
    if (client_ptr -> nx_websocket_client_use_tls)
    {
        status = nx_secure_tls_session_receive(client_ptr -> nx_websocket_client_tls_session_ptr, packet_ptr, wait_option);
    }
    else
    {
        status = nx_tcp_socket_receive(client_ptr -> nx_websocket_client_socket_ptr, packet_ptr, wait_option);
    }
#else
    status = nx_tcp_socket_receive(client_ptr -> nx_websocket_client_socket_ptr, packet_ptr, wait_option);
#endif /* NX_SECURE_ENABLE */

    /* Obtain the mutex again. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_connect_response_check         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks if a whole connect response is received, then  */
/*    process the response.                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    packet_ptr                            Pointer to packet             */ 
/*    wait_option                           Wait option                   */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nx_packet_data_append                 Append data into packet       */
/*    nx_packet_release                     Release packet                */
/*    _nx_websocket_client_connect_response_process                       */
/*                                          Process connect response      */
/*    _nx_websocket_client_cleanup          Cleanup resource              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_connect_internal Make websocket connection     */
/*    _nx_websocket_client_receive          Receive websocket data frame  */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_connect_response_check(NX_WEBSOCKET_CLIENT *client_ptr, NX_PACKET *packet_ptr, UINT wait_option)
{
CHAR *buffer_ptr;
UINT status = NX_SUCCESS;
UINT crlf_found = 0;
NX_PACKET *tmp_ptr;


    if (client_ptr -> nx_websocket_client_processing_packet == NX_NULL)
    {
        client_ptr -> nx_websocket_client_processing_packet = packet_ptr;
    }
    else
    {

        /* Its contents now need to be placed in the head packet.  */
        tmp_ptr = packet_ptr;

#ifndef NX_DISABLE_PACKET_CHAIN
        while (tmp_ptr)
#endif /* NX_DISABLE_PACKET_CHAIN */
        {

            /* Release the mutex */
            tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));

            /* Copy the contents of the current packet into the head packet.  */
            status = nx_packet_data_append(client_ptr -> nx_websocket_client_processing_packet,
                                           (VOID *) tmp_ptr -> nx_packet_prepend_ptr,
                                           (ULONG)(tmp_ptr -> nx_packet_append_ptr - tmp_ptr -> nx_packet_prepend_ptr),
                                           client_ptr -> nx_websocket_client_packet_pool_ptr, wait_option);

            /* Obtain the mutex. */
            tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

            /* Determine if an error occurred or an unexpected packet chain happens since the connect response shall not exceed the maximum length of one packet */
            if ((status != NX_SUCCESS) || (client_ptr -> nx_websocket_client_processing_packet -> nx_packet_next))
            {

                /* Reset the state to idle */
                client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_IDLE;

                /* Clean up unused resources. */
                _nx_websocket_client_cleanup(client_ptr);

                /* Release the new packet and return. */
                nx_packet_release(packet_ptr);
                return(status);
            }
#ifndef NX_DISABLE_PACKET_CHAIN
            else
            {
                tmp_ptr = tmp_ptr -> nx_packet_next;
            }
#endif /* NX_DISABLE_PACKET_CHAIN */
        }

        /* Release the new packet. */
        nx_packet_release(packet_ptr);
    }

    crlf_found = 0;
    tmp_ptr = client_ptr -> nx_websocket_client_processing_packet;

    while (1)
    {

        /* Build a pointer to the buffer area.  */
        buffer_ptr =  (CHAR *) tmp_ptr -> nx_packet_prepend_ptr;
    
        /* See if there is a blank line present in the buffer.  */
        /* Search the buffer for a cr/lf pair.  */
        while ((buffer_ptr < (CHAR *) tmp_ptr -> nx_packet_append_ptr) &&
               (crlf_found < 4))
        {
            if (!(crlf_found & 1) && (*buffer_ptr == (CHAR)13))
            {

                /* Found CR. */
                crlf_found++;
            }
            else if((crlf_found & 1) && (*buffer_ptr == (CHAR)10))
            {

                /* Found LF. */
                crlf_found++;
            }
            else
            {

                /* Reset the CRLF marker. */
                crlf_found = 0;
            }
    
            /* Move the buffer pointer up.  */
            buffer_ptr++;
        }

        if (crlf_found == 4)
        {

            /* Yes, we have found the end of the HTTP response header.  */
            break;
        }

#ifndef NX_DISABLE_PACKET_CHAIN

        if (tmp_ptr -> nx_packet_next != NX_NULL)
        {

            /* Get the next packet in the chain. */
            tmp_ptr  = tmp_ptr -> nx_packet_next;
        }
        else
#endif
        {
            return(NX_IN_PROGRESS);
        }
    }

    /* Process the response.  */
    status = _nx_websocket_client_connect_response_process(client_ptr, client_ptr -> nx_websocket_client_processing_packet);
    if (status == NX_SUCCESS)
    {

        /* Update the state.  */
        client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_CONNECTED;

        /* If connection is established, notify application.  */
        if (client_ptr -> nx_websocket_client_connection_status_callback)
        {
            client_ptr -> nx_websocket_client_connection_status_callback(client_ptr, client_ptr -> nx_websocket_client_connection_context, NX_SUCCESS);
        }
    }
    else
    {
        client_ptr -> nx_websocket_client_state = NX_WEBSOCKET_CLIENT_STATE_IDLE;
    }

    /* Clean up unused resources. */
    _nx_websocket_client_cleanup(client_ptr);

    return (status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_websocket_client_connection_status_callback_set                */
/*                                                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the WebSocket callback set.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    context                               Context for callback          */ 
/*    connection_status_callback            Routine to call when connect  */ 
/*                                            or disconnect occurs        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_websocket_client_connection_status_callback_set                 */
/*                                          Actual websocket callback set */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxe_websocket_client_connection_status_callback_set(NX_WEBSOCKET_CLIENT *client_ptr, VOID *context,
                                                           VOID (*connection_status_callback)(NX_WEBSOCKET_CLIENT *, VOID *, UINT))
{
UINT        status;


    /* Check for invalid input pointers.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_websocket_client_id != NX_WEBSOCKET_CLIENT_ID))
    {
        return(NX_PTR_ERROR);
    }

    /* Call actual process function.  */
    status = _nx_websocket_client_connection_status_callback_set(client_ptr, context, connection_status_callback);

    /* Return completion status.  */
    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_connection_status_callback_set PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sets Websocket connection callback.                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*    context                               Context for callback          */ 
/*    connection_status_callback            Routine to call when connect  */ 
/*                                            or disconnect occurs        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_websocket_client_connection_status_callback_set(NX_WEBSOCKET_CLIENT *client_ptr, VOID *context,
                                                          VOID (*connection_status_callback)(NX_WEBSOCKET_CLIENT *, VOID *, UINT))
{

    /* Obtain the mutex. */
    tx_mutex_get(&(client_ptr -> nx_websocket_client_mutex), NX_WAIT_FOREVER);

    /* Set the context which will be passed to connection status callback.  */
    client_ptr -> nx_websocket_client_connection_context = context;

    /* Set the connection status callback.  */
    client_ptr -> nx_websocket_client_connection_status_callback = connection_status_callback;

    /* Release the mutex */
    tx_mutex_put(&(client_ptr -> nx_websocket_client_mutex));

    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_websocket_client_cleanup                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Bo Chen, Microsoft Corporation                                      */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function cleanups resources.                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to WebSocket Client   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_websocket_client_delete           Delete websocket instance     */
/*    _nx_websocket_client_data_process     Process data frame            */
/*    _nx_websocket_client_connect_response_check                         */
/*                                          Check connect response        */
/*    _nx_websocket_client_connect_internal Make websocket connection     */
/*                                                                        */
/**************************************************************************/
void  _nx_websocket_client_cleanup(NX_WEBSOCKET_CLIENT *client_ptr)
{

    /* Reset the flag for frame header found */
    client_ptr -> nx_websocket_client_frame_header_found = NX_FALSE;

    /* Release the waiting list.  */
    if (client_ptr -> nx_websocket_client_processing_packet)
    {
        nx_packet_release(client_ptr -> nx_websocket_client_processing_packet);
        client_ptr -> nx_websocket_client_processing_packet = NX_NULL;
    }
}