Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usbd_cdc_rndis.c « Src « CDC_RNDIS « Class « STM32_USB_Device_Library « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4a08d35276d145101f6ab30f7336d8b500bebae2 (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
/**
  ******************************************************************************
  * @file    usbd_cdc_rndis.c
  * @author  MCD Application Team
  * @brief   This file provides the high layer firmware functions to manage the
  *          following functionalities of the USB CDC_RNDIS Class:
  *           - Initialization and Configuration of high and low layer
  *           - Enumeration as CDC_RNDIS Device (and enumeration for each implemented memory interface)
  *           - OUT/IN data transfer
  *           - Command IN transfer (class requests management)
  *           - Error management
  *
  *  @verbatim
  *
  *          ===================================================================
  *                                CDC_RNDIS Class Driver Description
  *          ===================================================================
  *           This driver manages the "Universal Serial Bus Class Definitions for Communications Devices
  *           Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus
  *           Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007"
  *           This driver implements the following aspects of the specification:
  *             - Device descriptor management
  *             - Configuration descriptor management
  *             - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN)
  *             - Requests management (as described in section 6.2 in specification)
  *             - Abstract Control Model compliant
  *             - Union Functional collection (using 1 IN endpoint for control)
  *             - Data interface class
  *
  *           These aspects may be enriched or modified for a specific user application.
  *
  *            This driver doesn't implement the following aspects of the specification
  *            (but it is possible to manage these features with some modifications on this driver):
  *             - Any class-specific aspect relative to communication classes should be managed by user application.
  *             - All communication classes other than PSTN are not managed
  *
  *  @endverbatim
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                      www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usbd_cdc_rndis.h"
#include "usbd_ctlreq.h"

#ifndef __USBD_CDC_RNDIS_IF_H
#include "usbd_cdc_rndis_if_template.h"
#endif
/** @addtogroup STM32_USB_DEVICE_LIBRARY
  * @{
  */


/** @defgroup USBD_CDC_RNDIS
  * @brief usbd core module
  * @{
  */

/** @defgroup USBD_CDC_RNDIS_Private_TypesDefinitions
  * @{
  */
/**
  * @}
  */


/** @defgroup USBD_CDC_RNDIS_Private_Defines
  * @{
  */
/**
  * @}
  */


/** @defgroup USBD_CDC_RNDIS_Private_Macros
  * @{
  */

/**
  * @}
  */


/** @defgroup USBD_CDC_RNDIS_Private_FunctionPrototypes
  * @{
  */

static uint8_t USBD_CDC_RNDIS_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CDC_RNDIS_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);

static uint8_t USBD_CDC_RNDIS_Setup(USBD_HandleTypeDef *pdev,
                                    USBD_SetupReqTypedef *req);

static uint8_t USBD_CDC_RNDIS_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_CDC_RNDIS_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_CDC_RNDIS_EP0_RxReady(USBD_HandleTypeDef *pdev);
static uint8_t *USBD_CDC_RNDIS_GetFSCfgDesc(uint16_t *length);
static uint8_t *USBD_CDC_RNDIS_GetHSCfgDesc(uint16_t *length);
static uint8_t *USBD_CDC_RNDIS_GetOtherSpeedCfgDesc(uint16_t *length);
static uint8_t *USBD_CDC_RNDIS_GetOtherSpeedCfgDesc(uint16_t *length);

#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
static uint8_t *USBD_CDC_RNDIS_USRStringDescriptor(USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *length);
#endif

uint8_t *USBD_CDC_RNDIS_GetDeviceQualifierDescriptor(uint16_t *length);


/* CDC_RNDIS Internal messages parsing and construction functions */
static uint8_t USBD_CDC_RNDIS_MsgParsing(USBD_HandleTypeDef *pdev, uint8_t *RxBuff);
static uint8_t USBD_CDC_RNDIS_ProcessInitMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_InitMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessHaltMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_HaltMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessKeepAliveMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_KpAliveMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessQueryMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_QueryMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessSetMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_SetMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessResetMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_ResetMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessPacketMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_PacketMsgTypeDef *Msg);
static uint8_t USBD_CDC_RNDIS_ProcessUnsupportedMsg(USBD_HandleTypeDef *pdev, USBD_CDC_RNDIS_CtrlMsgTypeDef *Msg);

/* USB Standard Device Descriptor */
__ALIGN_BEGIN static uint8_t USBD_CDC_RNDIS_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
{
  USB_LEN_DEV_QUALIFIER_DESC,
  USB_DESC_TYPE_DEVICE_QUALIFIER,
  0x00,
  0x02,
  0x00,
  0x00,
  0x00,
  0x40,
  0x01,
  0x00,
};

static uint8_t MAC_StrDesc[6] = {CDC_RNDIS_MAC_ADDR0, CDC_RNDIS_MAC_ADDR1, CDC_RNDIS_MAC_ADDR2,
                                 CDC_RNDIS_MAC_ADDR3, CDC_RNDIS_MAC_ADDR4, CDC_RNDIS_MAC_ADDR5
                                };

static uint32_t ConnSpeedTab[2] = {CDC_RNDIS_CONNECT_SPEED_UPSTREAM,
                                   CDC_RNDIS_CONNECT_SPEED_DOWNSTREAM
                                  };

static uint8_t EmptyResponse = 0x00U;

/**
  * @}
  */

/** @defgroup USBD_CDC_RNDIS_Private_Variables
  * @{
  */


/* CDC_RNDIS interface class callbacks structure */
USBD_ClassTypeDef USBD_CDC_RNDIS =
{
  USBD_CDC_RNDIS_Init,
  USBD_CDC_RNDIS_DeInit,
  USBD_CDC_RNDIS_Setup,
  NULL,                 /* EP0_TxSent, */
  USBD_CDC_RNDIS_EP0_RxReady,
  USBD_CDC_RNDIS_DataIn,
  USBD_CDC_RNDIS_DataOut,
  NULL,
  NULL,
  NULL,
  USBD_CDC_RNDIS_GetHSCfgDesc,
  USBD_CDC_RNDIS_GetFSCfgDesc,
  USBD_CDC_RNDIS_GetOtherSpeedCfgDesc,
  USBD_CDC_RNDIS_GetDeviceQualifierDescriptor,
#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
  USBD_CDC_RNDIS_USRStringDescriptor,
#endif
};

/* USB CDC_RNDIS device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_CDC_RNDIS_CfgHSDesc[] __ALIGN_END =
{
  /* Configuration Descriptor */
  0x09,                                        /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION,                 /* bDescriptorType: Configuration */
  LOBYTE(CDC_RNDIS_CONFIG_DESC_SIZ),           /* wTotalLength: Total size of the Config descriptor */
  HIBYTE(CDC_RNDIS_CONFIG_DESC_SIZ),
  0x02,                                        /* bNumInterfaces: 2 interface */
  0x01,                                        /* bConfigurationValue: Configuration value */
  0x00,                                        /* iConfiguration: Index of string descriptor describing the configuration */
#if (USBD_SELF_POWERED == 1U)
  0xC0,                                        /* bmAttributes: Bus Powered according to user configuration */
#else
  0x80,                                        /* bmAttributes: Bus Powered according to user configuration */
#endif
  USBD_MAX_POWER,                              /* MaxPower 100 mA */

  /*---------------------------------------------------------------------------*/
  /* IAD descriptor */
  0x08,                                        /* bLength */
  0x0B,                                        /* bDescriptorType */
  0x00,                                        /* bFirstInterface */
  0x02,                                        /* bInterfaceCount */
  0xE0,                                        /* bFunctionClass (Wireless Controller) */
  0x01,                                        /* bFunctionSubClass */
  0x03,                                        /* bFunctionProtocol */
  0x00,                                        /* iFunction */

  /*---------------------------------------------------------------------------*/
  /* Interface Descriptor */
  0x09,                                        /* bLength: Interface Descriptor size */
  USB_DESC_TYPE_INTERFACE,                     /* bDescriptorType: Interface descriptor type */
  CDC_RNDIS_CMD_ITF_NBR,                       /* bInterfaceNumber: Number of Interface */
  0x00,                                        /* bAlternateSetting: Alternate setting */
  0x01,                                        /* bNumEndpoints: One endpoint used */
  0x02,                                        /* bInterfaceClass: Communication Interface Class */
  0x02,                                        /* bInterfaceSubClass:Abstract Control Model */
  0xFF,                                        /* bInterfaceProtocol: Common AT commands */
  0x00,                                        /* iInterface: */

  /* Header Functional Descriptor */
  0x05,                                        /* bLength: Endpoint Descriptor size */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x00,                                        /* bDescriptorSubtype: Header functional descriptor */
  0x10,                                        /* bcdCDC: spec release number: 1.20 */
  0x01,

  /* Call Management Functional Descriptor */
  0x05,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x01,                                        /* bDescriptorSubtype: Call Management Func Desc */
  0x00,                                        /* bmCapabilities: D0+D1 */
  CDC_RNDIS_COM_ITF_NBR,                       /* bDataInterface: 1 */

  /* ACM Functional Descriptor */
  0x04,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x02,                                        /* bDescriptorSubtype: Abstract Control Management desc */
  0x00,                                        /* bmCapabilities */

  /* Union Functional Descriptor */
  0x05,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x06,                                        /* bDescriptorSubtype: Union functional descriptor */
  CDC_RNDIS_CMD_ITF_NBR,                       /* bMasterInterface: Communication class interface */
  CDC_RNDIS_COM_ITF_NBR,                       /* bSlaveInterface0: Data Class Interface */

  /* Notification Endpoint Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_CMD_EP,                            /* bEndpointAddress */
  0x03,                                        /* bmAttributes: Interrupt */
  LOBYTE(CDC_RNDIS_CMD_PACKET_SIZE),           /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_CMD_PACKET_SIZE),
  CDC_RNDIS_HS_BINTERVAL,                      /* bInterval */

  /*---------------------------------------------------------------------------*/
  /* Data class interface descriptor */
  0x09,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_INTERFACE,                     /* bDescriptorType: */
  CDC_RNDIS_COM_ITF_NBR,                       /* bInterfaceNumber: Number of Interface */
  0x00,                                        /* bAlternateSetting: Alternate setting */
  0x02,                                        /* bNumEndpoints: Two endpoints used */
  0x0A,                                        /* bInterfaceClass: CDC */
  0x00,                                        /* bInterfaceSubClass: */
  0x00,                                        /* bInterfaceProtocol: */
  0x00,                                        /* iInterface: */

  /* Endpoint OUT Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_OUT_EP,                            /* bEndpointAddress */
  0x02,                                        /* bmAttributes: Bulk */
  LOBYTE(CDC_RNDIS_DATA_HS_MAX_PACKET_SIZE),   /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_DATA_HS_MAX_PACKET_SIZE),
  0xFF,                                        /* bInterval: ignore for Bulk transfer */

  /* Endpoint IN Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_IN_EP,                             /* bEndpointAddress */
  0x02,                                        /* bmAttributes: Bulk */
  LOBYTE(CDC_RNDIS_DATA_HS_MAX_PACKET_SIZE),   /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_DATA_HS_MAX_PACKET_SIZE),
  0xFF                                         /* bInterval: ignore for Bulk transfer */
};


/* USB CDC device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t  USBD_CDC_RNDIS_CfgFSDesc[] __ALIGN_END =
{
  /* Configuration Descriptor */
  0x09,                                        /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION,                 /* bDescriptorType: Configuration */
  LOBYTE(CDC_RNDIS_CONFIG_DESC_SIZ),           /* wTotalLength: Total size of the Config descriptor */
  HIBYTE(CDC_RNDIS_CONFIG_DESC_SIZ),
  0x02,                                        /* bNumInterfaces: 2 interface */
  0x01,                                        /* bConfigurationValue: Configuration value */
  0x00,                                        /* iConfiguration: Index of string descriptor describing the configuration */
#if (USBD_SELF_POWERED == 1U)
  0xC0,                                        /* bmAttributes: Bus Powered according to user configuration */
#else
  0x80,                                        /* bmAttributes: Bus Powered according to user configuration */
#endif
  USBD_MAX_POWER,                              /* MaxPower 100 mA */

  /*---------------------------------------------------------------------------*/
  /* IAD descriptor */
  0x08,                                        /* bLength */
  0x0B,                                        /* bDescriptorType */
  0x00,                                        /* bFirstInterface */
  0x02,                                        /* bInterfaceCount */
  0xE0,                                        /* bFunctionClass (Wireless Controller) */
  0x01,                                        /* bFunctionSubClass */
  0x03,                                        /* bFunctionProtocol */
  0x00,                                        /* iFunction */

  /*---------------------------------------------------------------------------*/
  /* Interface Descriptor */
  0x09,                                        /* bLength: Interface Descriptor size */
  USB_DESC_TYPE_INTERFACE,                     /* bDescriptorType: Interface descriptor type */
  CDC_RNDIS_CMD_ITF_NBR,                       /* bInterfaceNumber: Number of Interface */
  0x00,                                        /* bAlternateSetting: Alternate setting */
  0x01,                                        /* bNumEndpoints: One endpoint used */
  0x02,                                        /* bInterfaceClass: Communication Interface Class */
  0x02,                                        /* bInterfaceSubClass:Abstract Control Model */
  0xFF,                                        /* bInterfaceProtocol: Common AT commands */
  0x00,                                        /* iInterface: */

  /* Header Functional Descriptor */
  0x05,                                        /* bLength: Endpoint Descriptor size */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x00,                                        /* bDescriptorSubtype: Header functional descriptor */
  0x10,                                        /* bcdCDC: spec release number: 1.20 */
  0x01,

  /* Call Management Functional Descriptor */
  0x05,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x01,                                        /* bDescriptorSubtype: Call Management Func Desc */
  0x00,                                        /* bmCapabilities: D0+D1 */
  CDC_RNDIS_COM_ITF_NBR,                       /* bDataInterface: 1 */

  /* ACM Functional Descriptor */
  0x04,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x02,                                        /* bDescriptorSubtype: Abstract Control Management desc */
  0x00,                                        /* bmCapabilities */

  /* Union Functional Descriptor */
  0x05,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x06,                                        /* bDescriptorSubtype: Union functional descriptor */
  CDC_RNDIS_CMD_ITF_NBR,                       /* bMasterInterface: Communication class interface */
  CDC_RNDIS_COM_ITF_NBR,                       /* bSlaveInterface0: Data Class Interface */

  /* Notification Endpoint Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_CMD_EP,                            /* bEndpointAddress */
  0x03,                                        /* bmAttributes: Interrupt */
  LOBYTE(CDC_RNDIS_CMD_PACKET_SIZE),           /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_CMD_PACKET_SIZE),
  CDC_RNDIS_FS_BINTERVAL,                      /* bInterval */

  /*---------------------------------------------------------------------------*/
  /* Data class interface descriptor */
  0x09,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_INTERFACE,                     /* bDescriptorType: */
  CDC_RNDIS_COM_ITF_NBR,                       /* bInterfaceNumber: Number of Interface */
  0x00,                                        /* bAlternateSetting: Alternate setting */
  0x02,                                        /* bNumEndpoints: Two endpoints used */
  0x0A,                                        /* bInterfaceClass: CDC */
  0x00,                                        /* bInterfaceSubClass: */
  0x00,                                        /* bInterfaceProtocol: */
  0x00,                                        /* iInterface: */

  /* Endpoint OUT Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_OUT_EP,                            /* bEndpointAddress */
  0x02,                                        /* bmAttributes: Bulk */
  LOBYTE(CDC_RNDIS_DATA_FS_MAX_PACKET_SIZE),   /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_DATA_FS_MAX_PACKET_SIZE),
  0xFF,                                        /* bInterval: ignore for Bulk transfer */

  /* Endpoint IN Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_IN_EP,                             /* bEndpointAddress */
  0x02,                                        /* bmAttributes: Bulk */
  LOBYTE(CDC_RNDIS_DATA_FS_MAX_PACKET_SIZE),   /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_DATA_FS_MAX_PACKET_SIZE),
  0xFF                                         /* bInterval: ignore for Bulk transfer */
} ;

__ALIGN_BEGIN static uint8_t USBD_CDC_RNDIS_OtherSpeedCfgDesc[] __ALIGN_END =
{
  /* Configuration Descriptor */
  0x09,                                        /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION,                 /* bDescriptorType: Configuration */
  LOBYTE(CDC_RNDIS_CONFIG_DESC_SIZ),           /* wTotalLength:no of returned bytes */
  HIBYTE(CDC_RNDIS_CONFIG_DESC_SIZ),
  0x02,                                        /* bNumInterfaces: 2 interface */
  0x01,                                        /* bConfigurationValue: Configuration value */
  0x04,                                        /* iConfiguration: Index of string descriptor describing the configuration */
#if (USBD_SELF_POWERED == 1U)
  0xC0,                                        /* bmAttributes: Bus Powered according to user configuration */
#else
  0x80,                                        /* bmAttributes: Bus Powered according to user configuration */
#endif
  USBD_MAX_POWER,                              /* MaxPower 100 mA */

  /*---------------------------------------------------------------------------*/
  /* IAD descriptor */
  0x08,                                        /* bLength */
  0x0B,                                        /* bDescriptorType */
  0x00,                                        /* bFirstInterface */
  0x02,                                        /* bInterfaceCount */
  0xE0,                                        /* bFunctionClass (Wireless Controller) */
  0x01,                                        /* bFunctionSubClass */
  0x03,                                        /* bFunctionProtocol */
  0x00,                                        /* iFunction */

  /*---------------------------------------------------------------------------*/
  /* Interface Descriptor */
  0x09,                                        /* bLength: Interface Descriptor size */
  USB_DESC_TYPE_INTERFACE,                     /* bDescriptorType: Interface descriptor type */
  CDC_RNDIS_CMD_ITF_NBR,                       /* bInterfaceNumber: Number of Interface */
  0x00,                                        /* bAlternateSetting: Alternate setting */
  0x01,                                        /* bNumEndpoints: One endpoint used */
  0x02,                                        /* bInterfaceClass: Communication Interface Class */
  0x02,                                        /* bInterfaceSubClass:Abstract Control Model */
  0xFF,                                        /* bInterfaceProtocol: Common AT commands */
  0x00,                                        /* iInterface: */

  /* Header Functional Descriptor */
  0x05,                                        /* bLength: Endpoint Descriptor size */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x00,                                        /* bDescriptorSubtype: Header functional descriptor */
  0x10,                                        /* bcdCDC: spec release number: 1.20 */
  0x01,

  /* Call Management Functional Descriptor */
  0x05,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x01,                                        /* bDescriptorSubtype: Call Management Func Desc */
  0x00,                                        /* bmCapabilities: D0+D1 */
  CDC_RNDIS_COM_ITF_NBR,                       /* bDataInterface: 1 */

  /* ACM Functional Descriptor */
  0x04,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x02,                                        /* bDescriptorSubtype: Abstract Control Management desc */
  0x00,                                        /* bmCapabilities */

  /* Union Functional Descriptor */
  0x05,                                        /* bFunctionLength */
  0x24,                                        /* bDescriptorType: CS_INTERFACE */
  0x06,                                        /* bDescriptorSubtype: Union functional descriptor */
  CDC_RNDIS_CMD_ITF_NBR,                       /* bMasterInterface: Communication class interface */
  CDC_RNDIS_COM_ITF_NBR,                       /* bSlaveInterface0: Data Class Interface */

  /* Communication Endpoint Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_CMD_EP,                            /* bEndpointAddress */
  0x03,                                        /* bmAttributes: Interrupt */
  LOBYTE(CDC_RNDIS_CMD_PACKET_SIZE),           /* wMaxPacketSize: */
  HIBYTE(CDC_RNDIS_CMD_PACKET_SIZE),
  CDC_RNDIS_FS_BINTERVAL,                      /* bInterval */

  /*---------------------------------------------------------------------------*/
  /* Data class interface descriptor */
  0x09,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_INTERFACE,                     /* bDescriptorType: */
  CDC_RNDIS_COM_ITF_NBR,                       /* bInterfaceNumber: Number of Interface */
  0x00,                                        /* bAlternateSetting: Alternate setting */
  0x02,                                        /* bNumEndpoints: Two endpoints used */
  0x0A,                                        /* bInterfaceClass: CDC */
  0x00,                                        /* bInterfaceSubClass: */
  0x00,                                        /* bInterfaceProtocol: */
  0x00,                                        /* iInterface: */

  /* Endpoint OUT Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_OUT_EP,                            /* bEndpointAddress */
  0x02,                                        /* bmAttributes: Bulk */
  0x40,                                        /* wMaxPacketSize: */
  0x00,
  0xFF,                                        /* bInterval: ignore for Bulk transfer */

  /* Endpoint IN Descriptor */
  0x07,                                        /* bLength: Endpoint Descriptor size */
  USB_DESC_TYPE_ENDPOINT,                      /* bDescriptorType: Endpoint */
  CDC_RNDIS_IN_EP,                             /* bEndpointAddress */
  0x02,                                        /* bmAttributes: Bulk */
  0x40,                                        /* wMaxPacketSize: */
  0x00,
  0xFF                                         /* bInterval: ignore for Bulk transfer */
};


static const uint32_t CDC_RNDIS_SupportedOIDs[] =
{
  OID_GEN_SUPPORTED_LIST,
  OID_GEN_HARDWARE_STATUS,
  OID_GEN_MEDIA_SUPPORTED,
  OID_GEN_MEDIA_IN_USE,
  OID_GEN_MAXIMUM_FRAME_SIZE,
  OID_GEN_LINK_SPEED,
  OID_GEN_TRANSMIT_BLOCK_SIZE,
  OID_GEN_RECEIVE_BLOCK_SIZE,
  OID_GEN_VENDOR_ID,
  OID_GEN_VENDOR_DESCRIPTION,
  OID_GEN_CURRENT_PACKET_FILTER,
  OID_GEN_MAXIMUM_TOTAL_SIZE,
  OID_GEN_MEDIA_CONNECT_STATUS,
  OID_GEN_MAXIMUM_SEND_PACKETS,
  OID_802_3_PERMANENT_ADDRESS,
  OID_802_3_CURRENT_ADDRESS,
  OID_802_3_MULTICAST_LIST,
  OID_802_3_MAXIMUM_LIST_SIZE,
  OID_802_3_RCV_ERROR_ALIGNMENT,
  OID_802_3_XMIT_ONE_COLLISION,
  OID_802_3_XMIT_MORE_COLLISIONS,
};

/**
  * @}
  */

/** @defgroup USBD_CDC_RNDIS_Private_Functions
  * @{
  */

/**
  * @brief  USBD_CDC_RNDIS_Init
  *         Initialize the CDC CDC_RNDIS interface
  * @param  pdev: device instance
  * @param  cfgidx: Configuration index
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
  UNUSED(cfgidx);
  USBD_CDC_RNDIS_HandleTypeDef *hcdc;

  hcdc = USBD_malloc(sizeof(USBD_CDC_RNDIS_HandleTypeDef));

  if (hcdc == NULL)
  {
    pdev->pClassData = NULL;
    return (uint8_t)USBD_EMEM;
  }

  pdev->pClassData = (void *)hcdc;

  if (pdev->dev_speed == USBD_SPEED_HIGH)
  {
    /* Open EP IN */
    (void)USBD_LL_OpenEP(pdev, CDC_RNDIS_IN_EP, USBD_EP_TYPE_BULK,
                         CDC_RNDIS_DATA_HS_IN_PACKET_SIZE);

    pdev->ep_in[CDC_RNDIS_IN_EP & 0xFU].is_used = 1U;

    /* Open EP OUT */
    (void)USBD_LL_OpenEP(pdev, CDC_RNDIS_OUT_EP, USBD_EP_TYPE_BULK,
                         CDC_RNDIS_DATA_HS_OUT_PACKET_SIZE);

    pdev->ep_out[CDC_RNDIS_OUT_EP & 0xFU].is_used = 1U;

    /* Set bInterval for CDC RNDIS CMD Endpoint */
    pdev->ep_in[CDC_RNDIS_CMD_EP & 0xFU].bInterval = CDC_RNDIS_HS_BINTERVAL;
  }
  else
  {
    /* Open EP IN */
    (void)USBD_LL_OpenEP(pdev, CDC_RNDIS_IN_EP, USBD_EP_TYPE_BULK,
                         CDC_RNDIS_DATA_FS_IN_PACKET_SIZE);

    pdev->ep_in[CDC_RNDIS_IN_EP & 0xFU].is_used = 1U;

    /* Open EP OUT */
    (void)USBD_LL_OpenEP(pdev, CDC_RNDIS_OUT_EP, USBD_EP_TYPE_BULK,
                         CDC_RNDIS_DATA_FS_OUT_PACKET_SIZE);

    pdev->ep_out[CDC_RNDIS_OUT_EP & 0xFU].is_used = 1U;

    /* Set bInterval for CDC RNDIS CMD Endpoint */
    pdev->ep_in[CDC_RNDIS_CMD_EP & 0xFU].bInterval = CDC_RNDIS_FS_BINTERVAL;
  }

  /* Open Command IN EP */
  (void)USBD_LL_OpenEP(pdev, CDC_RNDIS_CMD_EP, USBD_EP_TYPE_INTR, CDC_RNDIS_CMD_PACKET_SIZE);
  pdev->ep_in[CDC_RNDIS_CMD_EP & 0xFU].is_used = 1U;

  /* Init  physical Interface components */
  ((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->Init();

  /* Init the CDC_RNDIS state */
  hcdc->State = CDC_RNDIS_STATE_BUS_INITIALIZED;

  /* Init Xfer states */
  hcdc->TxState = 0U;
  hcdc->RxState = 0U;
  hcdc->RxLength = 0U;
  hcdc->TxLength = 0U;
  hcdc->LinkStatus = 0U;
  hcdc->NotificationStatus = 0U;
  hcdc->MaxPcktLen = (pdev->dev_speed == USBD_SPEED_HIGH) ? CDC_RNDIS_DATA_HS_MAX_PACKET_SIZE : CDC_RNDIS_DATA_FS_MAX_PACKET_SIZE;

  /* Prepare Out endpoint to receive next packet */
  (void)USBD_LL_PrepareReceive(pdev, CDC_RNDIS_OUT_EP,
                               hcdc->RxBuffer, hcdc->MaxPcktLen);

  return (uint8_t)USBD_OK;
}

/**
  * @brief  USBD_CDC_RNDIS_DeInit
  *         DeInitialize the CDC layer
  * @param  pdev: device instance
  * @param  cfgidx: Configuration index
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
  UNUSED(cfgidx);

  /* Close EP IN */
  (void)USBD_LL_CloseEP(pdev, CDC_RNDIS_IN_EP);
  pdev->ep_in[CDC_RNDIS_IN_EP & 0xFU].is_used = 0U;

  /* Close EP OUT */
  (void)USBD_LL_CloseEP(pdev, CDC_RNDIS_OUT_EP);
  pdev->ep_out[CDC_RNDIS_OUT_EP & 0xFU].is_used = 0U;

  /* Close Command IN EP */
  (void)USBD_LL_CloseEP(pdev, CDC_RNDIS_CMD_EP);
  pdev->ep_in[CDC_RNDIS_CMD_EP & 0xFU].is_used = 0U;
  pdev->ep_in[CDC_RNDIS_CMD_EP & 0xFU].bInterval = 0U;

  /* DeInit  physical Interface components */
  if (pdev->pClassData != NULL)
  {
    ((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->DeInit();
    USBD_free(pdev->pClassData);
    pdev->pClassData = NULL;
  }

  return (uint8_t)USBD_OK;
}

/**
  * @brief  USBD_CDC_RNDIS_Setup
  *         Handle the CDC specific requests
  * @param  pdev: instance
  * @param  req: usb requests
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_Setup(USBD_HandleTypeDef *pdev,
                                    USBD_SetupReqTypedef *req)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;
  USBD_CDC_RNDIS_CtrlMsgTypeDef *Msg = (USBD_CDC_RNDIS_CtrlMsgTypeDef *)(void *)hcdc->data;
  uint8_t ifalt = 0U;
  uint16_t status_info = 0U;
  USBD_StatusTypeDef ret = USBD_OK;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  switch (req->bmRequest & USB_REQ_TYPE_MASK)
  {
    case USB_REQ_TYPE_CLASS :
      if (req->wLength != 0U)
      {
        /* Control Request Data from Device to Host, send data prepared by device */
        if ((req->bmRequest & 0x80U) != 0U)
        {
          /* Update opcode and length */
          hcdc->CmdOpCode = req->bRequest;
          hcdc->CmdLength = (uint8_t)req->wLength;

          if (hcdc->CmdOpCode == CDC_RNDIS_GET_ENCAPSULATED_RESPONSE)
          {
            /* Data of Response Message has already been prepared by USBD_CDC_RNDIS_MsgParsing.
            Just check that length is corresponding to right expected value */
            if (req->wLength != Msg->MsgLength)
            {
            }
          }

          /* Allow application layer to pre-process data or add own processing before sending response */
          ((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest,
                                                                  (uint8_t *)hcdc->data,
                                                                  req->wLength);
          /* Check if Response is ready */
          if (hcdc->ResponseRdy != 0U)
          {
            /* Clear Response Ready flag */
            hcdc->ResponseRdy = 0U;

            /* Send data on control endpoint */
            (void)USBD_CtlSendData(pdev, (uint8_t *)hcdc->data, Msg->MsgLength);
          }
          else
          {
            /* CDC_RNDIS Specification says: If for some reason the device receives a GET ENCAPSULATED RESPONSE
            and is unable to respond with a valid data on the Control endpoint,
            then it should return a one-byte packet set to 0x00, rather than
            stalling the Control endpoint */
            (void)USBD_CtlSendData(pdev, &EmptyResponse, 1U);
          }
        }
        /* Control Request Data from Host to Device: Prepare reception of control data stage */
        else
        {
          hcdc->CmdOpCode = req->bRequest;
          hcdc->CmdLength = (uint8_t)req->wLength;

          (void)USBD_CtlPrepareRx(pdev, (uint8_t *)hcdc->data, req->wLength);
        }
      }
      /* No Data control request: there is no such request for CDC_RNDIS protocol,
      so let application layer manage this case */
      else
      {
        ((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest,
                                                                (uint8_t *)req, 0U);
      }
      break;

    case USB_REQ_TYPE_STANDARD:
      switch (req->bRequest)
      {
        case USB_REQ_GET_STATUS:
          if (pdev->dev_state == USBD_STATE_CONFIGURED)
          {
            (void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
          }
          else
          {
            USBD_CtlError(pdev, req);
            ret = USBD_FAIL;
          }
          break;

        case USB_REQ_GET_INTERFACE:
          if (pdev->dev_state == USBD_STATE_CONFIGURED)
          {
            (void)USBD_CtlSendData(pdev, &ifalt, 1U);
          }
          else
          {
            USBD_CtlError(pdev, req);
            ret = USBD_FAIL;
          }
          break;

        case USB_REQ_SET_INTERFACE:
          if (pdev->dev_state != USBD_STATE_CONFIGURED)
          {
            USBD_CtlError(pdev, req);
            ret = USBD_FAIL;
          }
          break;

        case USB_REQ_CLEAR_FEATURE:
          break;

        default:
          USBD_CtlError(pdev, req);
          ret = USBD_FAIL;
          break;
      }
      break;

    default:
      USBD_CtlError(pdev, req);
      ret = USBD_FAIL;
      break;
  }

  return (uint8_t)ret;
}

/**
  * @brief  USBD_CDC_RNDIS_DataIn
  *         Data sent on non-control IN endpoint
  * @param  pdev: device instance
  * @param  epnum: endpoint number
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc;
  PCD_HandleTypeDef *hpcd = pdev->pData;

  if (pdev->pClassData == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  if (epnum == (CDC_RNDIS_IN_EP & 0x7FU))
  {
    if ((pdev->ep_in[epnum & 0xFU].total_length > 0U) &&
        ((pdev->ep_in[epnum & 0xFU].total_length % hpcd->IN_ep[epnum & 0xFU].maxpacket) == 0U))
    {
      /* Update the packet total length */
      pdev->ep_in[epnum & 0xFU].total_length = 0U;

      /* Send ZLP */
      (void)USBD_LL_Transmit(pdev, epnum, NULL, 0U);
    }
    else
    {
      hcdc->TxState = 0U;

      if (((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->TransmitCplt != NULL)
      {
        ((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->TransmitCplt(hcdc->TxBuffer, &hcdc->TxLength, epnum);
      }
    }
  }
  else if (epnum == (CDC_RNDIS_CMD_EP & 0x7FU))
  {
    if (hcdc->NotificationStatus != 0U)
    {
      (void)USBD_CDC_RNDIS_SendNotification(pdev, CONNECTION_SPEED_CHANGE,
                                            0U, (uint8_t *)ConnSpeedTab);

      hcdc->NotificationStatus = 0U;
    }
  }
  else
  {
    return (uint8_t)USBD_FAIL;
  }

  return (uint8_t)USBD_OK;
}

/**
  * @brief  USBD_CDC_RNDIS_DataOut
  *         Data received on non-control Out endpoint
  * @param  pdev: device instance
  * @param  epnum: endpoint number
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc;
  uint32_t CurrPcktLen;

  if (pdev->pClassData == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  if (epnum == CDC_RNDIS_OUT_EP)
  {
    /* Get the received data length */
    CurrPcktLen = USBD_LL_GetRxDataSize(pdev, epnum);

    /* Increment the frame length */
    hcdc->RxLength += CurrPcktLen;

    /* If the buffer size is less than max packet size: it is the last packet in current frame */
    if ((CurrPcktLen < hcdc->MaxPcktLen) ||
        (hcdc->RxLength >= (CDC_RNDIS_ETH_MAX_SEGSZE + sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef))))
    {
      /* USB data will be immediately processed, this allow next USB traffic being
      NAKed till the end of the application Xfer */

      /* Call data packet message parsing and processing function */
      (void)USBD_CDC_RNDIS_ProcessPacketMsg(pdev, (USBD_CDC_RNDIS_PacketMsgTypeDef *)(void *)hcdc->RxBuffer);
    }
    else
    {
      /* Prepare Out endpoint to receive next packet in current/new frame */
      (void)USBD_LL_PrepareReceive(pdev, CDC_RNDIS_OUT_EP,
                                   (uint8_t *)(hcdc->RxBuffer + hcdc->RxLength),
                                   hcdc->MaxPcktLen);
    }
  }
  else
  {
    return (uint8_t)USBD_FAIL;
  }

  return (uint8_t)USBD_OK;
}

/**
  * @brief  USBD_CDC_RNDIS_EP0_RxReady
  *         Handle EP0 Rx Ready event
  * @param  pdev: device instance
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_EP0_RxReady(USBD_HandleTypeDef *pdev)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  if ((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU))
  {
    /* Check if the received command is SendEncapsulated command */
    if (hcdc->CmdOpCode == CDC_RNDIS_SEND_ENCAPSULATED_COMMAND)
    {
      /* Process Received CDC_RNDIS Control Message */
      (void)USBD_CDC_RNDIS_MsgParsing(pdev, (uint8_t *)(hcdc->data));

      /* Reset the command opcode for next processing */
      hcdc->CmdOpCode = 0xFFU;
    }
    else
    {
      /* Reset the command opcode for next processing */
      hcdc->CmdOpCode = 0xFFU;

      /* Ignore the command and return fail */
      return (uint8_t)USBD_FAIL;
    }
  }

  return (uint8_t)USBD_OK;
}

/**
  * @brief  USBD_CDC_RNDIS_GetFSCfgDesc
  *         Return configuration descriptor
  * @param  speed : current device speed
  * @param  length : pointer data length
  * @retval pointer to descriptor buffer
  */
static uint8_t *USBD_CDC_RNDIS_GetFSCfgDesc(uint16_t *length)
{
  *length = (uint16_t)(sizeof(USBD_CDC_RNDIS_CfgFSDesc));

  return USBD_CDC_RNDIS_CfgFSDesc;
}

/**
  * @brief  USBD_CDC_RNDIS_GetHSCfgDesc
  *         Return configuration descriptor
  * @param  speed : current device speed
  * @param  length : pointer data length
  * @retval pointer to descriptor buffer
  */
static uint8_t *USBD_CDC_RNDIS_GetHSCfgDesc(uint16_t *length)
{
  *length = (uint16_t)(sizeof(USBD_CDC_RNDIS_CfgHSDesc));

  return USBD_CDC_RNDIS_CfgHSDesc;
}

/**
  * @brief  USBD_CDC_RNDIS_GetOtherSpeedCfgDesc
  *         Return configuration descriptor
  * @param  speed : current device speed
  * @param  length : pointer data length
  * @retval pointer to descriptor buffer
  */
static uint8_t *USBD_CDC_RNDIS_GetOtherSpeedCfgDesc(uint16_t *length)
{
  *length = (uint16_t)(sizeof(USBD_CDC_RNDIS_OtherSpeedCfgDesc));

  return USBD_CDC_RNDIS_OtherSpeedCfgDesc;
}

/**
  * @brief  DeviceQualifierDescriptor
  *         return Device Qualifier descriptor
  * @param  length : pointer data length
  * @retval pointer to descriptor buffer
  */
uint8_t *USBD_CDC_RNDIS_GetDeviceQualifierDescriptor(uint16_t *length)
{
  *length = (uint16_t)(sizeof(USBD_CDC_RNDIS_DeviceQualifierDesc));

  return USBD_CDC_RNDIS_DeviceQualifierDesc;
}

/**
  * @brief  USBD_CDC_RNDIS_RegisterInterface
  * @param  pdev: device instance
  * @param  fops: CD  Interface callback
  * @retval status
  */
uint8_t USBD_CDC_RNDIS_RegisterInterface(USBD_HandleTypeDef *pdev,
                                         USBD_CDC_RNDIS_ItfTypeDef *fops)
{
  if (fops == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  pdev->pUserData = fops;

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_USRStringDescriptor
  *         Manages the transfer of user string descriptors.
  * @param  speed : current device speed
  * @param  index: descriptor index
  * @param  length : pointer data length
  * @retval pointer to the descriptor table or NULL if the descriptor is not supported.
  */
#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
static uint8_t *USBD_CDC_RNDIS_USRStringDescriptor(USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *length)
{
  static uint8_t USBD_StrDesc[255];

  /* Check if the requested string interface is supported */
  if (index == CDC_RNDIS_MAC_STRING_INDEX)
  {
    USBD_GetString((uint8_t *)((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->pStrDesc, USBD_StrDesc, length);
    return USBD_StrDesc;
  }
  /* Not supported Interface Descriptor index */
  else
  {
    return NULL;
  }
}
#endif /* USBD_SUPPORT_USER_STRING_DESC */

/**
  * @brief  USBD_CDC_RNDIS_SetTxBuffer
  * @param  pdev: device instance
  * @param  pbuff: Tx Buffer
  * @retval status
  */
uint8_t USBD_CDC_RNDIS_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint32_t length)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  hcdc->TxBuffer = pbuff;
  hcdc->TxLength = length;

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_SetRxBuffer
  * @param  pdev: device instance
  * @param  pbuff: Rx Buffer
  * @retval status
  */
uint8_t USBD_CDC_RNDIS_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  hcdc->RxBuffer = pbuff;

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_TransmitPacket
  *         Transmit packet on IN endpoint
  * @param  pdev: device instance
  * @retval status
  */
uint8_t USBD_CDC_RNDIS_TransmitPacket(USBD_HandleTypeDef *pdev)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc;
  USBD_CDC_RNDIS_PacketMsgTypeDef *PacketMsg;
  USBD_StatusTypeDef ret = USBD_BUSY;

  if (pdev->pClassData == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;
  PacketMsg = (USBD_CDC_RNDIS_PacketMsgTypeDef *)(void *)hcdc->TxBuffer;

  if (hcdc->TxState == 0U)
  {
    /* Tx Transfer in progress */
    hcdc->TxState = 1U;

    /* Format the packet information */
    PacketMsg->MsgType = CDC_RNDIS_PACKET_MSG_ID;
    PacketMsg->MsgLength = hcdc->TxLength;
    PacketMsg->DataOffset = sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef) - CDC_RNDIS_PCKTMSG_DATAOFFSET_OFFSET;
    PacketMsg->DataLength = hcdc->TxLength - sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef);
    PacketMsg->OOBDataOffset = 0U;
    PacketMsg->OOBDataLength = 0U;
    PacketMsg->NumOOBDataElements = 0U;
    PacketMsg->PerPacketInfoOffset = 0U;
    PacketMsg->PerPacketInfoLength = 0U;
    PacketMsg->VcHandle = 0U;
    PacketMsg->Reserved = 0U;

    /* Update the packet total length */
    pdev->ep_in[CDC_RNDIS_IN_EP & 0xFU].total_length = hcdc->TxLength;

    /* Transmit next packet */
    (void)USBD_LL_Transmit(pdev, CDC_RNDIS_IN_EP, hcdc->TxBuffer, hcdc->TxLength);

    ret = USBD_OK;
  }

  return (uint8_t)ret;
}


/**
  * @brief  USBD_CDC_RNDIS_ReceivePacket
  *         prepare OUT Endpoint for reception
  * @param  pdev: device instance
  * @retval status
  */
uint8_t USBD_CDC_RNDIS_ReceivePacket(USBD_HandleTypeDef *pdev)
{
  USBD_CDC_RNDIS_HandleTypeDef *hcdc;

  if (pdev->pClassData == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Prepare Out endpoint to receive next packet */
  (void)USBD_LL_PrepareReceive(pdev, CDC_RNDIS_OUT_EP,
                               hcdc->RxBuffer, hcdc->MaxPcktLen);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_SendNotification
  *         Transmit Notification packet on CMD IN interrupt endpoint
  * @param  pdev: device instance
  *         Notif: value of the notification type (from CDC_RNDIS_Notification_TypeDef enumeration list)
  *         bVal: value of the notification switch (ie. 0x00 or 0x01 for Network Connection notification)
  *         pData: pointer to data buffer (ie. upstream and downstream connection speed values)
  * @retval status
  */
uint8_t USBD_CDC_RNDIS_SendNotification(USBD_HandleTypeDef *pdev,
                                        USBD_CDC_RNDIS_NotifCodeTypeDef Notif,
                                        uint16_t bVal, uint8_t *pData)
{
  uint32_t Idx;
  uint16_t ReqSize = 0U;
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;
  USBD_StatusTypeDef ret = USBD_OK;

  UNUSED(bVal);
  UNUSED(pData);

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Initialize the request fields */
  (hcdc->Req).bmRequest = CDC_RNDIS_BMREQUEST_TYPE_RNDIS;
  (hcdc->Req).bRequest = (uint8_t)Notif;

  switch (Notif)
  {
    case RESPONSE_AVAILABLE:
      (hcdc->Req).wValue = 0U;
      (hcdc->Req).wIndex = CDC_RNDIS_CMD_ITF_NBR;
      (hcdc->Req).wLength = 0U;

      for (Idx = 0U; Idx < 8U; Idx++)
      {
        (hcdc->Req).data[Idx] = 0U;
      }

      ReqSize = 8U;
      break;

    default:
      ret = USBD_FAIL;
      break;
  }

  /* Transmit notification packet */
  if (ReqSize != 0U)
  {
    (void)USBD_LL_Transmit(pdev, CDC_RNDIS_CMD_EP, (uint8_t *) &(hcdc->Req), ReqSize);
  }

  return (uint8_t)ret;
}


/* ----------------------------- CDC_RNDIS Messages processing functions ----------------------- */
/**
  * @brief  USBD_CDC_RNDIS_MsgParsing
  *         Parse received message and process it depending on its nature.
  * @param  pdev: USB Device Handle pointer
  * @param  RxBuff: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_MsgParsing(USBD_HandleTypeDef *pdev, uint8_t *RxBuff)
{
  USBD_CDC_RNDIS_CtrlMsgTypeDef *Msg = (USBD_CDC_RNDIS_CtrlMsgTypeDef *)(void *)RxBuff;
  static uint8_t ret = (uint8_t)USBD_OK;

  /* Check message type */
  switch (Msg->MsgType)
  {
    /* CDC_RNDIS Initialize message */
    case CDC_RNDIS_INITIALIZE_MSG_ID:
      ret = USBD_CDC_RNDIS_ProcessInitMsg(pdev, (USBD_CDC_RNDIS_InitMsgTypeDef *)(void *)Msg);
      break;

    /* CDC_RNDIS Halt message */
    case CDC_RNDIS_HALT_MSG_ID:
      ret = USBD_CDC_RNDIS_ProcessHaltMsg(pdev, (USBD_CDC_RNDIS_HaltMsgTypeDef *)(void *)Msg);
      break;

    /* CDC_RNDIS Query message */
    case CDC_RNDIS_QUERY_MSG_ID:
      ret = USBD_CDC_RNDIS_ProcessQueryMsg(pdev, (USBD_CDC_RNDIS_QueryMsgTypeDef *)(void *)Msg);
      break;

    /* CDC_RNDIS Set message */
    case CDC_RNDIS_SET_MSG_ID:
      ret = USBD_CDC_RNDIS_ProcessSetMsg(pdev, (USBD_CDC_RNDIS_SetMsgTypeDef *)(void *)Msg);
      break;

    /* CDC_RNDIS Reset message */
    case CDC_RNDIS_RESET_MSG_ID:
      ret = USBD_CDC_RNDIS_ProcessResetMsg(pdev, (USBD_CDC_RNDIS_ResetMsgTypeDef *)(void *)Msg);
      break;

    /* CDC_RNDIS Keep-Alive message */
    case CDC_RNDIS_KEEPALIVE_MSG_ID:
      ret = USBD_CDC_RNDIS_ProcessKeepAliveMsg(pdev, (USBD_CDC_RNDIS_KpAliveMsgTypeDef *)(void *)Msg);
      break;

    /* CDC_RNDIS unsupported message */
    default:
      ret = USBD_CDC_RNDIS_ProcessUnsupportedMsg(pdev, (USBD_CDC_RNDIS_CtrlMsgTypeDef *)(void *)Msg);
      break;
  }

  return ret;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessInitMsg
  *         Parse, extract data and check correctness of CDC_RNDIS INIT_MSG command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessInitMsg(USBD_HandleTypeDef *pdev,
                                             USBD_CDC_RNDIS_InitMsgTypeDef *Msg)
{
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Get and format the Msg input */
  USBD_CDC_RNDIS_InitMsgTypeDef *InitMessage = (USBD_CDC_RNDIS_InitMsgTypeDef *)Msg;

  /* Use same Msg input buffer as response buffer */
  USBD_CDC_RNDIS_InitCpltMsgTypeDef *InitResponse = (USBD_CDC_RNDIS_InitCpltMsgTypeDef *)(void *)Msg;

  /* Store the Message Request ID */
  uint32_t ReqId = InitMessage->ReqId;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Check correctness of the message (MsgType already checked by entry to this function) */
  if ((InitMessage->MsgLength != sizeof(USBD_CDC_RNDIS_InitMsgTypeDef)) || \
      (InitMessage->MajorVersion < CDC_RNDIS_VERSION_MAJOR))
  {
    InitResponse->Status = CDC_RNDIS_STATUS_FAILURE;
  }
  else
  {
    InitResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
  }

  /* Setup the response buffer content */
  InitResponse->MsgType = CDC_RNDIS_INITIALIZE_CMPLT_ID;
  InitResponse->MsgLength = sizeof(USBD_CDC_RNDIS_InitCpltMsgTypeDef);
  InitResponse->ReqId = ReqId;
  InitResponse->MajorVersion = CDC_RNDIS_VERSION_MAJOR;
  InitResponse->MinorVersion = CDC_RNDIS_VERSION_MINOR;
  InitResponse->DeviceFlags = CDC_RNDIS_DF_CONNECTIONLESS;
  InitResponse->Medium = CDC_RNDIS_MEDIUM_802_3;
  InitResponse->MaxPacketsPerTransfer = 1U;
  InitResponse->MaxTransferSize = (sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef) + CDC_RNDIS_ETH_FRAME_SIZE_MAX);
  InitResponse->PacketAlignmentFactor = 2U; /* Not needed as single packet by transfer set */
  InitResponse->AFListOffset = 0U; /* Reserved for connection-oriented devices. Set value to zero. */
  InitResponse->AFListSize = 0U; /* Reserved for connection-oriented devices. Set value to zero. */

  /* Set CDC_RNDIS state to INITIALIZED */
  hcdc->State = CDC_RNDIS_STATE_INITIALIZED;

  /* Set Response Ready field in order to send response during next control request */
  hcdc->ResponseRdy = 1U;

  /* Send Notification on Interrupt EP to inform Host that response is ready */
  (void)USBD_CDC_RNDIS_SendNotification(pdev, RESPONSE_AVAILABLE, 0U, NULL);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessHaltMsg
  *         Parse, extract data and check correctness of CDC_RNDIS Halt command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessHaltMsg(USBD_HandleTypeDef *pdev,
                                             USBD_CDC_RNDIS_HaltMsgTypeDef *Msg)
{
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Set CDC_RNDIS state to INITIALIZED */
  hcdc->State = CDC_RNDIS_STATE_UNINITIALIZED;

  /* No response required for this message, so no notification (RESPONSE_AVAILABLE) is sent */

  UNUSED(Msg);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessKeepAliveMsg
  *         Parse, extract data and check correctness of CDC_RNDIS KeepAlive command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessKeepAliveMsg(USBD_HandleTypeDef *pdev,
                                                  USBD_CDC_RNDIS_KpAliveMsgTypeDef *Msg)
{
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Use same Msg input buffer as response buffer */
  USBD_CDC_RNDIS_KpAliveCpltMsgTypeDef *InitResponse = (USBD_CDC_RNDIS_KpAliveCpltMsgTypeDef *)(void *)Msg;

  /* Store the Message Request ID */
  uint32_t ReqId = Msg->ReqId;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Check correctness of the message (MsgType already checked by entry to this function) */
  if (Msg->MsgLength != sizeof(USBD_CDC_RNDIS_KpAliveMsgTypeDef))
  {
    InitResponse->Status = CDC_RNDIS_STATUS_FAILURE;
  }
  else
  {
    InitResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
  }

  /* Setup the response buffer content */
  InitResponse->MsgType = CDC_RNDIS_KEEPALIVE_CMPLT_ID;
  InitResponse->MsgLength = sizeof(USBD_CDC_RNDIS_KpAliveCpltMsgTypeDef);
  InitResponse->ReqId = ReqId;
  InitResponse->Status = CDC_RNDIS_STATUS_SUCCESS;

  /* Set Response Ready field in order to send response during next control request */
  hcdc->ResponseRdy = 1U;

  /* Send Notification on Interrupt EP to inform Host that response is ready */
  (void)USBD_CDC_RNDIS_SendNotification(pdev, RESPONSE_AVAILABLE, 0U, NULL);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessQueryMsg
  *         Parse, extract data and check correctness of CDC_RNDIS Query command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessQueryMsg(USBD_HandleTypeDef *pdev,
                                              USBD_CDC_RNDIS_QueryMsgTypeDef *Msg)
{
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Use same Msg input buffer as response buffer */
  USBD_CDC_RNDIS_QueryCpltMsgTypeDef *QueryResponse = (USBD_CDC_RNDIS_QueryCpltMsgTypeDef *)(void *)Msg;

  /* Store the Message Request ID */
  uint32_t ReqId = Msg->RequestId;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Process the OID depending on its code */
  switch (Msg->Oid)
  {
    case OID_GEN_SUPPORTED_LIST:
      QueryResponse->InfoBufLength = sizeof(CDC_RNDIS_SupportedOIDs);
      (void)USBD_memcpy(QueryResponse->InfoBuf, CDC_RNDIS_SupportedOIDs,
                        sizeof(CDC_RNDIS_SupportedOIDs));

      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_HARDWARE_STATUS:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = CDC_RNDIS_HW_STS_READY;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_MEDIA_SUPPORTED:
    case OID_GEN_MEDIA_IN_USE:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = CDC_RNDIS_MEDIUM_802_3;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_VENDOR_ID:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = USBD_CDC_RNDIS_VID;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_MAXIMUM_FRAME_SIZE:
    case OID_GEN_TRANSMIT_BLOCK_SIZE:
    case OID_GEN_RECEIVE_BLOCK_SIZE:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = CDC_RNDIS_ETH_FRAME_SIZE_MAX;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_VENDOR_DESCRIPTION:
      QueryResponse->InfoBufLength = (strlen(USBD_CDC_RNDIS_VENDOR_DESC) + 1U);
      (void)USBD_memcpy(QueryResponse->InfoBuf, USBD_CDC_RNDIS_VENDOR_DESC,
                        strlen(USBD_CDC_RNDIS_VENDOR_DESC));

      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_MEDIA_CONNECT_STATUS:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = CDC_RNDIS_MEDIA_STATE_CONNECTED;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_MAXIMUM_SEND_PACKETS:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = 1U;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_LINK_SPEED:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = USBD_CDC_RNDIS_LINK_SPEED;
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_802_3_PERMANENT_ADDRESS:
    case OID_802_3_CURRENT_ADDRESS:
      QueryResponse->InfoBufLength = 6U;
      (void)USBD_memcpy(QueryResponse->InfoBuf, MAC_StrDesc, 6);
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_802_3_MAXIMUM_LIST_SIZE:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = 1U; /* Only one multicast address supported */
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_CURRENT_PACKET_FILTER:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = 0xFFFFFFU; /* USBD_CDC_RNDIS_DEVICE.packetFilter; */
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_802_3_RCV_ERROR_ALIGNMENT:
    case OID_802_3_XMIT_ONE_COLLISION:
    case OID_802_3_XMIT_MORE_COLLISIONS:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      QueryResponse->InfoBuf[0] = 0U; /* Unused OIDs, return zero */
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_GEN_MAXIMUM_TOTAL_SIZE:
      QueryResponse->InfoBufLength = sizeof(uint32_t);
      /* Indicate maximum overall buffer (Ethernet frame and CDC_RNDIS header) the adapter can handle */
      QueryResponse->InfoBuf[0] = (CDC_RNDIS_MESSAGE_BUFFER_SIZE + CDC_RNDIS_ETH_FRAME_SIZE_MAX);
      QueryResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    default:
      /* Unknown or unsupported OID */
      QueryResponse->InfoBufLength = 0U;
      QueryResponse->Status = CDC_RNDIS_STATUS_FAILURE;
      break;
  }

  /* Setup the response buffer content */
  QueryResponse->MsgType = CDC_RNDIS_QUERY_CMPLT_ID;
  QueryResponse->MsgLength = QueryResponse->InfoBufLength + 24U;
  QueryResponse->ReqId = ReqId;
  QueryResponse->InfoBufOffset = 16U;

  /* Set Response Ready field in order to send response during next control request */
  hcdc->ResponseRdy = 1U;

  /* Send Notification on Interrupt EP to inform Host that response is ready */
  (void)USBD_CDC_RNDIS_SendNotification(pdev, RESPONSE_AVAILABLE, 0U, NULL);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessSetMsg
  *         Parse, extract data and check correctness of CDC_RNDIS Set Message command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessSetMsg(USBD_HandleTypeDef *pdev,
                                            USBD_CDC_RNDIS_SetMsgTypeDef *Msg)
{
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Get and format the Msg input */
  USBD_CDC_RNDIS_SetMsgTypeDef *SetMessage = (USBD_CDC_RNDIS_SetMsgTypeDef *)Msg;

  /* Use same Msg input buffer as response buffer */
  USBD_CDC_RNDIS_SetCpltMsgTypeDef *SetResponse = (USBD_CDC_RNDIS_SetCpltMsgTypeDef *)(void *)Msg;

  /* Store the Message Request ID */
  uint32_t ReqId = SetMessage->ReqId;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  switch (SetMessage->Oid)
  {
    case OID_GEN_CURRENT_PACKET_FILTER:
      /* Setup the packet filter value */
      hcdc->PacketFilter = SetMessage->InfoBuf[0];
      SetResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    case OID_802_3_MULTICAST_LIST:
      /* List of multicast addresses on a miniport adapter */
      SetResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
      break;

    default:
      /* Report an error */
      SetResponse->Status = CDC_RNDIS_STATUS_FAILURE;
      break;
  }

  /* Prepare response buffer */
  SetResponse->MsgType = CDC_RNDIS_SET_CMPLT_ID;
  SetResponse->MsgLength = sizeof(USBD_CDC_RNDIS_SetCpltMsgTypeDef);
  SetResponse->ReqId = ReqId;

  /* Set Response Ready field in order to send response during next control request */
  hcdc->ResponseRdy = 1U;

  /* Send Notification on Interrupt EP to inform Host that response is ready */
  (void)USBD_CDC_RNDIS_SendNotification(pdev, RESPONSE_AVAILABLE, 0U, NULL);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessResetMsg
  *         Parse, extract data and check correctness of CDC_RNDIS Set Message command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessResetMsg(USBD_HandleTypeDef *pdev,
                                              USBD_CDC_RNDIS_ResetMsgTypeDef *Msg)
{
  /* Get and format the Msg input */
  USBD_CDC_RNDIS_ResetMsgTypeDef *ResetMessage = (USBD_CDC_RNDIS_ResetMsgTypeDef *)Msg;
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;
  /* Use same Msg input buffer as response buffer */
  USBD_CDC_RNDIS_ResetCpltMsgTypeDef *ResetResponse = (USBD_CDC_RNDIS_ResetCpltMsgTypeDef *)(void *)Msg;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  if ((ResetMessage->MsgLength != sizeof(USBD_CDC_RNDIS_ResetMsgTypeDef)) || \
      (ResetMessage->Reserved != 0U))
  {
    ResetResponse->Status = CDC_RNDIS_STATUS_FAILURE;
  }
  else
  {
    ResetResponse->Status = CDC_RNDIS_STATUS_SUCCESS;
  }

  /* Prepare response buffer */
  ResetResponse->MsgType = CDC_RNDIS_RESET_CMPLT_ID;
  ResetResponse->MsgLength = sizeof(USBD_CDC_RNDIS_ResetCpltMsgTypeDef);
  ResetResponse->AddrReset = 0U;

  /* Set CDC_RNDIS state to INITIALIZED */
  hcdc->State = CDC_RNDIS_STATE_BUS_INITIALIZED;
  hcdc->LinkStatus = 0U;

  /* Set Response Ready field in order to send response during next control request */
  hcdc->ResponseRdy = 1U;

  /* Send Notification on Interrupt EP to inform Host that response is ready */
  (void)USBD_CDC_RNDIS_SendNotification(pdev, RESPONSE_AVAILABLE, 0U, NULL);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessPacketMsg
  *         Parse, extract data and check correctness of CDC_RNDIS Data Packet.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from Packet
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessPacketMsg(USBD_HandleTypeDef *pdev,
                                               USBD_CDC_RNDIS_PacketMsgTypeDef *Msg)
{
  uint32_t tmp1, tmp2;

  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Get and format the Msg input */
  USBD_CDC_RNDIS_PacketMsgTypeDef *PacketMsg = (USBD_CDC_RNDIS_PacketMsgTypeDef *)Msg;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Check correctness of the message */
  if ((PacketMsg->MsgType != CDC_RNDIS_PACKET_MSG_ID))
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Point to the payload and update the message length */

  /* Use temporary storage variables to comply with MISRA-C 2012 rule of (+) operand allowed types */
  tmp1 = (uint32_t)PacketMsg;
  tmp2 = (uint32_t)(PacketMsg->DataOffset);
  hcdc->RxBuffer = (uint8_t *)(tmp1 + tmp2 + CDC_RNDIS_PCKTMSG_DATAOFFSET_OFFSET);
  hcdc->RxLength = PacketMsg->DataLength;

  /* Process data by application */
  ((USBD_CDC_RNDIS_ItfTypeDef *)pdev->pUserData)->Receive(hcdc->RxBuffer, &hcdc->RxLength);

  return (uint8_t)USBD_OK;
}


/**
  * @brief  USBD_CDC_RNDIS_ProcessUnsupportedMsg
  *         Parse, extract data and check correctness of CDC_RNDIS KeepAlive command.
  * @param  pdev: USB Device Handle pointer
  * @param  Msg: Pointer to the message data extracted from SendEncapsulated command
  * @retval status
  */
static uint8_t USBD_CDC_RNDIS_ProcessUnsupportedMsg(USBD_HandleTypeDef *pdev,
                                                    USBD_CDC_RNDIS_CtrlMsgTypeDef *Msg)
{
  /* Get the CDC_RNDIS handle pointer */
  USBD_CDC_RNDIS_HandleTypeDef *hcdc = (USBD_CDC_RNDIS_HandleTypeDef *)pdev->pClassData;

  /* Use same Msg input buffer as response buffer */
  USBD_CDC_RNDIS_StsChangeMsgTypeDef *Response = (USBD_CDC_RNDIS_StsChangeMsgTypeDef *)(void *)Msg;

  if (hcdc == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }

  /* Setup the response buffer content */
  Response->MsgType = CDC_RNDIS_INDICATE_STATUS_MSG_ID;
  Response->MsgLength = sizeof(USBD_CDC_RNDIS_StsChangeMsgTypeDef);
  Response->Status = CDC_RNDIS_STATUS_NOT_SUPPORTED;
  Response->StsBufLength = 0U;
  Response->StsBufOffset = 20U;

  /* Set Response Ready field in order to send response during next control request */
  hcdc->ResponseRdy = 1U;

  /* Send Notification on Interrupt EP to inform Host that response is ready */
  (void)USBD_CDC_RNDIS_SendNotification(pdev, RESPONSE_AVAILABLE, 0U, NULL);

  UNUSED(Msg);

  return (uint8_t)USBD_OK;
}


/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/