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
//! Rust safe wrapper for the [FTDI D2XX drivers].
//!
//! This takes the [libftd2xx-ffi] C bindings crate and extends it with rust
//! safe wrappers.
//!
//! # Usage
//! Simply add this crate as a dependency in your `Cargo.toml`.
//! On Linux the static library is distributed in the [libftd2xx-ffi] crate with
//! permission from FTDI.
//!
//! ```toml
//! [dependencies]
//! libftd2xx = "~0.24.1"
//! ```
//!
//! This is a basic example to get your started.
//! Check the source code or documentation for more examples.
//! ```no_run
//! use libftd2xx::{Ftdi, FtdiCommon};
//!
//! let mut ft = Ftdi::new()?;
//! let info = ft.device_info()?;
//! println!("Device information: {:?}", info);
//! # Ok::<(), libftd2xx::FtStatus>(())
//! ```
//!
//! ## One-time Linux Setup
//! To access the FTDI USB device as a regular user you need to update the
//! [udev] rules.
//!
//! Create a file called `/etc/udev/rules.d/99-ftdi.rules` with:
//! ```text
//! SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0666"
//! SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", MODE="0666"
//! SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", MODE="0666"
//! SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", MODE="0666"
//! SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", MODE="0666"
//! ```
//!
//! Then, reload the rules:
//! ```bash
//! sudo udevadm control --reload-rules
//! sudo udevadm trigger
//! ```
//!
//! ## One-time Windows Setup
//! Unlike Linux the Windows vendor driver is dynamically linked.
//! The FTD2XX DLL must exist on your system PATH.
//! The easiest way to install this is with the vendor provided [setup executable].
//!
//! # References
//!
//! * [D2XX Programmers Guide V1.4]
//! * [D2XX Drivers Download Page]
//!
//! # Troubleshooting
//! ## Unknown Device on Linux
//! Remove the VCP FTDI driver.
//! ```bash
//! sudo rmmod ftdi_sio
//! sudo rmmod usbserial
//! ```
//! See [FTDI Drivers Installation Guide for Linux] for more details.
//!
//! [D2XX Drivers Download Page]: https://www.ftdichip.com/Drivers/D2XX.htm
//! [D2xx Programmers Guide V1.4]: https://www.ftdichip.com/Support/Documents/ProgramGuides/D2XX_Programmer's_Guide(FT_000071).pdf
//! [FTDI D2XX drivers]: https://www.ftdichip.com/Drivers/D2XX.htm
//! [FTDI Drivers Installation Guide for Linux]: http://www.ftdichip.cn/Support/Documents/AppNotes/AN_220_FTDI_Drivers_Installation_Guide_for_Linux.pdf
//! [libftd2xx-ffi]: https://github.com/newAM/libftd2xx-ffi-rs
//! [setup executable]: https://www.ftdichip.com/Drivers/CDM/CDM21228_Setup.zip
//! [udev]: https://en.wikipedia.org/wiki/Udev
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc(html_root_url = "https://docs.rs/libftd2xx/0.24.1")]
#![deny(missing_docs)]

mod errors;
pub use errors::{DeviceTypeError, EepromStringsError, EepromValueError, FtStatus, TimeoutError};

mod mpsse;
pub use mpsse::{
    ClockBits, ClockBitsIn, ClockBitsOut, ClockData, ClockDataIn, ClockDataOut, FtdiMpsse,
    Ftx232hMpsse, MpsseCmdBuilder, MpsseSettings,
};

mod types;
use types::{vid_pid_from_id, STRING_LEN};
pub use types::{
    BitMode, BitsPerWord, ByteOrder, Cbus232h, Cbus232r, CbusX, ClockPolarity, DeviceInfo,
    DeviceStatus, DeviceType, DriveCurrent, DriverType, Eeprom232h, Eeprom4232h, EepromHeader,
    EepromStrings, ModemStatus, Parity, Speed, StopBits, Version,
};

mod util;
use util::slice_into_string;

use libftd2xx_ffi::{
    FT_Close, FT_ClrDtr, FT_ClrRts, FT_CreateDeviceInfoList, FT_EEPROM_Program, FT_EEPROM_Read,
    FT_EE_UARead, FT_EE_UASize, FT_EE_UAWrite, FT_EraseEE, FT_GetBitMode, FT_GetDeviceInfo,
    FT_GetDeviceInfoList, FT_GetDriverVersion, FT_GetLatencyTimer, FT_GetLibraryVersion,
    FT_GetModemStatus, FT_GetQueueStatus, FT_GetStatus, FT_ListDevices, FT_Open, FT_OpenEx,
    FT_Purge, FT_Read, FT_ReadEE, FT_ResetDevice, FT_SetBaudRate, FT_SetBitMode, FT_SetBreakOff,
    FT_SetBreakOn, FT_SetChars, FT_SetDataCharacteristics, FT_SetDeadmanTimeout, FT_SetDtr,
    FT_SetFlowControl, FT_SetLatencyTimer, FT_SetRts, FT_SetTimeouts, FT_SetUSBParameters,
    FT_Write, FT_WriteEE, FT_DEVICE_LIST_INFO_NODE, FT_EEPROM_232H, FT_EEPROM_4232H,
    FT_FLOW_DTR_DSR, FT_FLOW_NONE, FT_FLOW_RTS_CTS, FT_FLOW_XON_XOFF, FT_HANDLE,
    FT_LIST_NUMBER_ONLY, FT_OPEN_BY_DESCRIPTION, FT_OPEN_BY_SERIAL_NUMBER, FT_PURGE_RX,
    FT_PURGE_TX, FT_STATUS,
};

#[cfg(target_os = "windows")]
use libftd2xx_ffi::{FT_CyclePort, FT_GetComPortNumber, FT_Rescan, FT_ResetPort};

#[cfg(any(target_os = "linux", target_os = "mac"))]
use libftd2xx_ffi::{FT_GetVIDPID, FT_SetVIDPID};

use log::trace;
use std::convert::TryFrom;
use std::ffi::c_void;
use std::fs;
use std::io;
use std::mem;
use std::os::raw::c_char;
use std::path::Path;
use std::time::Duration;
use std::vec::Vec;

/// FTDI USB vendor id.
pub const FTDI_VID: u16 = 0x0403;

fn ft_result<T>(value: T, status: FT_STATUS) -> Result<T, FtStatus> {
    if status != 0 {
        Err(status.into())
    } else {
        Ok(value)
    }
}

/// Returns the number of FTDI devices connected to the system.
///
/// # Example
///
/// ```no_run
/// use libftd2xx::num_devices;
///
/// let num_devices = num_devices()?;
/// println!("Number of devices: {}", num_devices);
/// # Ok::<(), libftd2xx::FtStatus>(())
/// ```
pub fn num_devices() -> Result<u32, FtStatus> {
    let mut num_devs: u32 = 0;
    let dummy = std::ptr::null_mut();
    trace!(
        "FT_ListDevices({}, NULL, {})",
        num_devs,
        FT_LIST_NUMBER_ONLY
    );
    let status: FT_STATUS = unsafe {
        FT_ListDevices(
            &mut num_devs as *mut u32 as *mut c_void,
            dummy,
            FT_LIST_NUMBER_ONLY,
        )
    };

    ft_result(num_devs, status)
}

/// A command to include a custom VID and PID combination within the internal
/// device list table.
///
/// This function is available on Linux or mac only.
///
/// This will allow the driver to load for the specified VID and PID
/// combination.
///
/// # Example
///
/// ```no_run
/// use libftd2xx::{set_vid_pid, vid_pid};
///
/// set_vid_pid(0x1234, 0x1234)?;
/// let (vid, pid) = vid_pid()?;
/// assert_eq!(vid, 0x1234);
/// assert_eq!(pid, 0x1234);
/// # Ok::<(), libftd2xx::FtStatus>(())
/// ```
#[cfg(any(unix, doc))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
pub fn set_vid_pid(vid: u16, pid: u16) -> Result<(), FtStatus> {
    trace!("FT_SetVIDPID({}, {})", vid, pid);
    let status: FT_STATUS = unsafe { FT_SetVIDPID(vid.into(), pid.into()) };
    ft_result((), status)
}

/// A command to retrieve the current VID and PID combination from within the
/// internal device list table.
///
/// This function is available on Linux or mac only.
///
/// This `vid` and `pid` are set by [`set_vid_pid`].
///
/// **Note** this returns a tuple of `(u32, u32)`, these should be `u16` but
/// the underlying type in the FTD2XX driver is a `u32`, and the choice to
/// convert is left up to the user.
///
/// # Example
///
/// ```no_run
/// use libftd2xx::vid_pid;
///
/// let (vid, pid) = vid_pid()?;
/// println!("VID: 0x{:04X}", vid);
/// println!("PID: 0x{:04X}", vid);
/// # Ok::<(), libftd2xx::FtStatus>(())
/// ```
///
/// [`set_vid_pid`]: ./fn.set_vid_pid.html
#[cfg(any(unix, doc))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
pub fn vid_pid() -> Result<(u32, u32), FtStatus> {
    let mut vid: u32 = 0;
    let mut pid: u32 = 0;
    trace!("FT_GetVIDPID(_, _)");
    let status: FT_STATUS = unsafe { FT_GetVIDPID(&mut vid, &mut pid) };
    ft_result((vid, pid), status)
}

/// Returns the version of the underlying C library.
///
/// **Note**: The documentation says this function is only supported on Windows
/// but it seems to work correctly on Linux.
///
/// # Example
///
/// ```no_run
/// use libftd2xx::library_version;
///
/// let version = library_version()?;
/// println!("libftd2xx C library version: {}", version);
/// # Ok::<(), libftd2xx::FtStatus>(())
/// ```
pub fn library_version() -> Result<Version, FtStatus> {
    let mut version: u32 = 0;
    trace!("FT_GetLibraryVersion(_)");
    let status: FT_STATUS = unsafe { FT_GetLibraryVersion(&mut version) };

    ft_result(Version::with_raw(version), status)
}

fn create_device_info_list() -> Result<u32, FtStatus> {
    let mut num_devices: u32 = 0;
    trace!("FT_CreateDeviceInfoList(_)");
    let status: FT_STATUS = unsafe { FT_CreateDeviceInfoList(&mut num_devices) };
    ft_result(num_devices, status)
}

/// This function returns a device information vector with information about
/// the D2XX devices connected to the system.
///
/// # Example
///
/// ```no_run
/// use libftd2xx::list_devices;
///
/// let mut devices = list_devices()?;
///
/// while let Some(device) = devices.pop() {
///     println!("device: {:?}", device);
/// }
/// # Ok::<(), libftd2xx::FtStatus>(())
/// ```
pub fn list_devices() -> Result<Vec<DeviceInfo>, FtStatus> {
    let mut devices: Vec<DeviceInfo> = Vec::new();
    let mut num_devices: u32 = create_device_info_list()?;
    let num_devices_usize: usize = usize::try_from(num_devices).unwrap();
    if num_devices == 0 {
        return Ok(devices);
    }

    let mut list_info_vec: Vec<FT_DEVICE_LIST_INFO_NODE> = vec![
        FT_DEVICE_LIST_INFO_NODE {
            Flags: 0,
            Type: 0,
            ID: 0,
            LocId: 0,
            SerialNumber: [0; 16],
            Description: [0; 64],
            ftHandle: std::ptr::null_mut(),
        };
        num_devices_usize
    ];

    trace!("FT_GetDeviceInfoList(_, _)");
    let status: FT_STATUS = unsafe {
        FT_GetDeviceInfoList(
            list_info_vec.as_mut_ptr() as *mut FT_DEVICE_LIST_INFO_NODE,
            &mut num_devices,
        )
    };

    if status != 0 {
        Err(status.into())
    } else {
        while let Some(info_node) = list_info_vec.pop() {
            let (vid, pid) = vid_pid_from_id(info_node.ID);
            devices.push(DeviceInfo {
                port_open: info_node.Flags & 0x1 == 0x1,
                speed: Some((info_node.Flags & 0x2).into()),
                device_type: info_node.Type.into(),
                product_id: pid,
                vendor_id: vid,
                serial_number: slice_into_string(info_node.SerialNumber.as_ref()),
                description: slice_into_string(info_node.Description.as_ref()),
            });
        }
        devices.sort_unstable();
        Ok(devices)
    }
}

/// Lists FTDI devices using the Linux file system.
///
/// There is a bug in the vendor driver where the `serial_number` and
/// `description` fields may be blank on the FT4232H and FT2232H when only
/// some of the ports are unbound from the `ftdi_sio` linux kernel module.
///
/// This will not work if you have a custom VID/PID programmed onto your FTDI
/// device.
///
/// # Limitations
///
/// * `port_open` will always be `false`.
/// * `speed` will currently be `None`.
/// * This will return an empty vector if `/sys/bus/usb/devices` does not exist.
///
/// # Example
///
/// ```no_run
/// use libftd2xx::list_devices_fs;
///
/// let mut devices = list_devices_fs()?;
///
/// while let Some(device) = devices.pop() {
///     println!("device: {:?}", device);
/// }
/// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
/// ```
#[allow(clippy::redundant_field_names)]
pub fn list_devices_fs() -> io::Result<Vec<DeviceInfo>> {
    let sys_bus_usb_devices = Path::new("/sys/bus/usb/devices");
    let mut devices: Vec<DeviceInfo> = Vec::new();
    if sys_bus_usb_devices.is_dir() {
        for entry in fs::read_dir(sys_bus_usb_devices)? {
            let entry = entry?;
            let path = entry.path();
            let mut vendor_path = path.clone();
            vendor_path.push("idVendor");
            if vendor_path.is_file() {
                let vid: String = fs::read_to_string(vendor_path)?;
                let vid: u16 = u16::from_str_radix(vid.trim(), 16)
                    .expect("idVendor file contains non-hex digits");
                if vid != FTDI_VID {
                    continue;
                }
            } else {
                continue;
            }

            let mut product_path = path.clone();
            product_path.push("idProduct");
            let pid: String = fs::read_to_string(product_path)?;
            let pid: u16 = u16::from_str_radix(pid.trim(), 16)
                .expect("idProduct file contains non-hex digits");

            let device_type: DeviceType = match DeviceType::with_pid(pid) {
                Some(device_type) => device_type,
                None => continue,
            };

            let serial: String = {
                let mut serial_path = path.clone();
                serial_path.push("serial");
                let mut data: String = fs::read_to_string(serial_path)?;
                let ch = data.pop(); // remove newline
                debug_assert_eq!(ch, Some('\n'));
                data
            };

            let description: String = {
                let mut product_path = path.clone();
                product_path.push("product");
                let mut data: String = fs::read_to_string(product_path)?;
                let ch = data.pop(); // remove newline
                debug_assert_eq!(ch, Some('\n'));
                data
            };

            let port_letters: Option<&'static [char]> = match device_type {
                DeviceType::FT2232H => Some(&['A', 'B']),
                DeviceType::FT4232H => Some(&['A', 'B', 'C', 'D']),
                _ => None,
            };

            if let Some(port_letters) = port_letters {
                for letter in port_letters {
                    let mut port_serial = serial.clone();
                    port_serial.push(*letter);
                    let mut port_description = description.clone();
                    port_description.push(' ');
                    port_description.push(*letter);
                    devices.push(DeviceInfo {
                        port_open: false,
                        speed: None,
                        device_type: device_type,
                        product_id: pid,
                        vendor_id: FTDI_VID,
                        serial_number: port_serial,
                        description: port_description,
                    })
                }
            } else {
                devices.push(DeviceInfo {
                    port_open: false,
                    speed: None,
                    device_type: device_type,
                    product_id: pid,
                    vendor_id: FTDI_VID,
                    serial_number: serial,
                    description: description,
                })
            }
        }

        devices.sort_unstable();
        Ok(devices)
    } else {
        // windows
        Ok(devices)
    }
}

/// Rescan devices on the system.
///
/// This function is available on Windows only.
///
/// This can be of used when trying to recover devices programatically.
///
/// Calling `rescan` is equivalent to clicking the "Scan for hardware changes"
/// button in the Device Manager.
/// Only USB hardware is checked for new devices.
/// All USB devices are scanned, not just FTDI devices.
///
/// # Example
///
/// ```no_run
/// libftd2xx::rescan()?;
/// # Ok::<(), libftd2xx::FtStatus>(())
/// ```
#[cfg(all(any(windows, doc), not(doctest)))]
#[cfg_attr(docsrs, doc(cfg(windows)))]
pub fn rescan() -> Result<(), FtStatus> {
    trace!("FT_Rescan()");
    let status: FT_STATUS = unsafe { FT_Rescan() };
    ft_result((), status)
}

/// Generic FTDI device.
///
/// This structure can be used for all FTDI devices.
/// A device-specific structure is only necessary to access the EEPROM traits
/// for that device.
pub struct Ftdi {
    handle: FT_HANDLE,
}

/// FT232H device.
///
/// # Example
///
/// Converting from an unknown FTDI device.
///
/// ```no_run
/// use libftd2xx::{Ft232h, Ftdi};
/// use std::convert::TryFrom;
///
/// let mut ftdi = Ftdi::new()?;
/// let ft232h: Ft232h = Ft232h::try_from(&mut ftdi)?;
/// # Ok::<(), libftd2xx::DeviceTypeError>(())
/// ```
pub struct Ft232h {
    handle: FT_HANDLE,
}

/// FT4232H device.
///
/// # Example
///
/// Converting from an unknown FTDI device.
///
/// ```no_run
/// use libftd2xx::{Ft4232h, Ftdi};
/// use std::convert::TryFrom;
///
/// let mut ftdi = Ftdi::new()?;
/// let ft4232h: Ft4232h = Ft4232h::try_from(&mut ftdi)?;
/// # Ok::<(), libftd2xx::DeviceTypeError>(())
/// ```
pub struct Ft4232h {
    handle: FT_HANDLE,
}

/// FTD2XX functions common to all devices.
pub trait FtdiCommon {
    /// FTDI device type.
    const DEVICE_TYPE: DeviceType;

    /// Get the FTDI device handle.
    fn handle(&mut self) -> FT_HANDLE;

    /// Get device information for an open device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let info = ft.device_info()?;
    /// println!("Device information: {:?}", info);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn device_info(&mut self) -> Result<DeviceInfo, FtStatus> {
        let mut device_type: u32 = 0;
        let mut device_id: u32 = 0;
        let mut serial_number: [i8; STRING_LEN] = [0; STRING_LEN];
        let mut description: [i8; STRING_LEN] = [0; STRING_LEN];
        trace!("FT_GetDeviceInfo({:?}, _, _, _, _, NULL)", self.handle());
        let status: FT_STATUS = unsafe {
            FT_GetDeviceInfo(
                self.handle(),
                &mut device_type,
                &mut device_id,
                serial_number.as_mut_ptr() as *mut c_char,
                description.as_mut_ptr() as *mut c_char,
                std::ptr::null_mut(),
            )
        };
        let (vid, pid) = vid_pid_from_id(device_id);
        ft_result(
            DeviceInfo {
                port_open: true,
                speed: None,
                device_type: device_type.into(),
                product_id: pid,
                vendor_id: vid,
                serial_number: slice_into_string(serial_number.as_ref()),
                description: slice_into_string(description.as_ref()),
            },
            status,
        )
    }

    /// Returns the D2XX driver version number.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let version = ft.driver_version()?;
    /// println!("Driver Version: {}", version);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn driver_version(&mut self) -> Result<Version, FtStatus> {
        let mut version: u32 = 0;
        trace!("FT_GetDriverVersion({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_GetDriverVersion(self.handle(), &mut version) };

        ft_result(Version::with_raw(version), status)
    }

    /// This function sends a reset command to the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.reset()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn reset(&mut self) -> Result<(), FtStatus> {
        trace!("FT_ResetDevice({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_ResetDevice(self.handle()) };
        ft_result((), status)
    }

    /// Set the USB request transfer size.
    ///
    /// This function can be used to change the transfer sizes from the default
    /// transfer size of 4096 bytes to better suit the application requirements.
    ///
    /// Transfer sizes must be set to a multiple of 64 bytes between 64 bytes
    /// and 64k bytes.  Other values will result in panic.
    ///
    /// When [`set_usb_parameters`] is called, the change comes into effect
    /// immediately and any data that was held in the driver at the time of the
    /// change is lost.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_usb_parameters(16384)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`set_usb_parameters`]: #method.set_usb_parameters
    fn set_usb_parameters(&mut self, in_transfer_size: u32) -> Result<(), FtStatus> {
        const MAX: u32 = 64 * 1024;
        const MIN: u32 = 64;
        assert!(
            in_transfer_size <= MAX,
            "in_transfer_size of {} exceeds maximum of {}",
            in_transfer_size,
            MAX
        );
        assert!(
            in_transfer_size >= MIN,
            "in_transfer_size of {} exceeds minimum of {}",
            in_transfer_size,
            MIN
        );
        assert!(
            in_transfer_size % MIN == 0,
            "in_transfer_size of {} not a multiple of {}",
            in_transfer_size,
            MIN
        );
        trace!(
            "FT_SetUSBParameters({:?}, {}, {})",
            self.handle(),
            in_transfer_size,
            in_transfer_size
        );
        let status: FT_STATUS =
            unsafe { FT_SetUSBParameters(self.handle(), in_transfer_size, in_transfer_size) };
        ft_result((), status)
    }

    /// This function sets the special characters for the device.
    ///
    /// This function allows for inserting specified characters in the data
    /// stream to represent events firing or errors occurring.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    ///
    /// // disable all special characters
    /// ft.set_chars(0, false, 0, false)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_chars(
        &mut self,
        event_char: u8,
        event_enable: bool,
        error_char: u8,
        error_enable: bool,
    ) -> Result<(), FtStatus> {
        trace!(
            "FT_SetChars({:?}, {}, {}, {}, {})",
            self.handle(),
            event_char,
            u8::try_from(event_enable).unwrap(),
            error_char,
            u8::try_from(error_enable).unwrap()
        );
        let status: FT_STATUS = unsafe {
            FT_SetChars(
                self.handle(),
                event_char,
                u8::try_from(event_enable).unwrap(),
                error_char,
                u8::try_from(error_enable).unwrap(),
            )
        };
        ft_result((), status)
    }

    /// This function sets the read and write timeouts for the device.
    ///
    /// The timeout values are limited to 4,294,967,295 (`std::u32::MAX`)
    /// milliseconds.
    ///
    /// The timeout values have a 1 millisecond resolution.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    /// use std::time::Duration;
    ///
    /// let mut ft = Ftdi::new()?;
    ///
    /// // Set read timeout of 5sec, write timeout of 1sec
    /// ft.set_timeouts(Duration::from_millis(5000), Duration::from_millis(1000))?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_timeouts(
        &mut self,
        read_timeout: Duration,
        write_timeout: Duration,
    ) -> Result<(), FtStatus> {
        let read_timeout_ms =
            u32::try_from(read_timeout.as_millis()).expect("read_timeout integer overflow");
        let write_timeout_ms =
            u32::try_from(write_timeout.as_millis()).expect("write_timeout integer overflow");
        trace!(
            "FT_SetTimeouts({:?}, {}, {})",
            self.handle(),
            read_timeout_ms,
            write_timeout_ms,
        );
        let status: FT_STATUS =
            unsafe { FT_SetTimeouts(self.handle(), read_timeout_ms, write_timeout_ms) };
        ft_result((), status)
    }

    /// This method allows the maximum time in milliseconds that a USB request
    /// can remain outstandingto be set.
    ///
    /// The deadman timeout is referred to in FTDI application note
    /// [AN232B-10 Advanced Driver Options] as the USB timeout.
    /// It is unlikely that this method will be required by most users.
    ///
    /// The default duration is 5 seconds.
    ///
    /// The timeout value is limited to 4,294,967,295 (`std::u32::MAX`)
    /// milliseconds.
    ///
    /// The timeout value has a 1 millisecond resolution.
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    /// use std::time::Duration;
    ///
    /// let mut ft = Ftdi::new()?;
    ///
    /// // set deadman timeout to 5 seconds
    /// ft.set_deadman_timeout(Duration::from_secs(5))?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [AN232B-10 Advanced Driver Options]: https://www.ftdichip.com/Support/Documents/AppNotes/AN_107_AdvancedDriverOptions_AN_000073.pdf
    fn set_deadman_timeout(&mut self, timeout: Duration) -> Result<(), FtStatus> {
        let timeout_ms = u32::try_from(timeout.as_millis()).expect("timeout integer overflow");
        trace!("FT_SetDeadmanTimeout({:?}, {})", self.handle(), timeout_ms);
        let status: FT_STATUS = unsafe { FT_SetDeadmanTimeout(self.handle(), timeout_ms) };
        ft_result((), status)
    }

    /// Set the latency timer value.
    ///
    /// In the FT8U232AM and FT8U245AM devices, the receive buffer timeout that
    /// is used to flush remaining data from the receive buffer was fixed at
    /// 16 ms.
    /// In all other FTDI devices, this timeout is programmable and can be set
    /// at 1 ms intervals between 2ms and 255 ms.  This allows the device to be
    /// better optimized for protocols requiring faster response times from
    /// short data packets.
    ///
    /// The valid range for the latency timer is 2 to 255 milliseconds.
    ///
    /// The resolution for the latency timer is 1 millisecond.
    ///
    /// **Note** the python FTDI library, [pyftdi] reports that values lower
    /// than 16 ms may result in data loss.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    /// use std::time::Duration;
    ///
    /// let mut ft = Ftdi::new()?;
    ///
    /// // Set latency timer to 16 milliseconds
    /// ft.set_latency_timer(Duration::from_millis(16))?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [pyftdi]: https://github.com/eblot/pyftdi/tree/master
    fn set_latency_timer(&mut self, timer: Duration) -> Result<(), FtStatus> {
        let millis = timer.as_millis();
        debug_assert!(millis >= 2, "duration must be >= 2ms, got {:?}", timer);
        debug_assert!(millis <= 255, "duration must be <= 255ms, got {:?}", timer);
        let millis = u8::try_from(millis).unwrap();
        trace!("FT_SetLatencyTimer({:?}, {})", self.handle(), millis);
        let status: FT_STATUS = unsafe { FT_SetLatencyTimer(self.handle(), millis) };
        ft_result((), status)
    }

    /// Get the current value of the latency timer.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    /// use std::time::Duration;
    ///
    /// let mut ft = Ftdi::new()?;
    /// let timer = Duration::from_millis(32);
    /// ft.set_latency_timer(timer)?;
    /// assert_eq!(ft.latency_timer()?, timer);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn latency_timer(&mut self) -> Result<Duration, FtStatus> {
        let mut timer: u8 = 0;
        trace!("FT_GetLatencyTimer({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_GetLatencyTimer(self.handle(), &mut timer as *mut u8) };
        ft_result(Duration::from_millis(timer as u64), status)
    }

    /// This function disables flow control for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_flow_control_none()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_flow_control_none(&mut self) -> Result<(), FtStatus> {
        trace!(
            "FT_SetFlowControl({:?}, {}, 0, 0)",
            self.handle(),
            FT_FLOW_NONE
        );
        let status: FT_STATUS =
            unsafe { FT_SetFlowControl(self.handle(), FT_FLOW_NONE as u16, 0, 0) };

        ft_result((), status)
    }

    /// This function sets RTS/CTS flow control for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_flow_control_rts_cts()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_flow_control_rts_cts(&mut self) -> Result<(), FtStatus> {
        trace!(
            "FT_SetFlowControl({:?}, {}, 0, 0)",
            self.handle(),
            FT_FLOW_RTS_CTS
        );
        let status: FT_STATUS =
            unsafe { FT_SetFlowControl(self.handle(), FT_FLOW_RTS_CTS as u16, 0, 0) };

        ft_result((), status)
    }

    /// This function sets DTS/DSR flow control for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_flow_control_dtr_dsr()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_flow_control_dtr_dsr(&mut self) -> Result<(), FtStatus> {
        trace!(
            "FT_SetFlowControl({:?}, {}, 0, 0)",
            self.handle(),
            FT_FLOW_DTR_DSR
        );
        let status: FT_STATUS =
            unsafe { FT_SetFlowControl(self.handle(), FT_FLOW_DTR_DSR as u16, 0, 0) };

        ft_result((), status)
    }

    /// This function sets XON/XOFF flow control for the device.
    ///
    /// # Arguments
    ///
    /// * `xon` - Character used to signal Xon.
    /// * `xoff` - Character used to signal Xoff.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_flow_control_xon_xoff(0x11, 0x13)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_flow_control_xon_xoff(&mut self, xon: u8, xoff: u8) -> Result<(), FtStatus> {
        trace!(
            "FT_SetFlowControl({:?}, {}, {}, {})",
            self.handle(),
            FT_FLOW_XON_XOFF,
            xon,
            xoff
        );
        let status: FT_STATUS =
            unsafe { FT_SetFlowControl(self.handle(), FT_FLOW_XON_XOFF as u16, xon, xoff) };

        ft_result((), status)
    }

    /// Set the baud rate for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_baud_rate(115200)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_baud_rate(&mut self, baud_rate: u32) -> Result<(), FtStatus> {
        trace!("FT_SetBaudRate({:?}, {})", self.handle(), baud_rate);
        let status: FT_STATUS = unsafe { FT_SetBaudRate(self.handle(), baud_rate) };
        ft_result((), status)
    }

    /// Set the data characteristics for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{BitsPerWord, Ftdi, FtdiCommon, Parity, StopBits};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_data_characteristics(BitsPerWord::Bits8, StopBits::Bits1, Parity::No)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_data_characteristics(
        &mut self,
        bits_per_word: BitsPerWord,
        stop_bits: StopBits,
        parity: Parity,
    ) -> Result<(), FtStatus> {
        trace!(
            "FT_SetDataCharacteristics({:?}, {}, {}, {})",
            self.handle(),
            u8::from(bits_per_word),
            u8::from(stop_bits),
            u8::from(parity),
        );
        let status: FT_STATUS = unsafe {
            FT_SetDataCharacteristics(
                self.handle(),
                bits_per_word.into(),
                stop_bits.into(),
                parity.into(),
            )
        };
        ft_result((), status)
    }

    /// Set the Data Terminal Ready (DTR) control signal.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_dtr()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_dtr(&mut self) -> Result<(), FtStatus> {
        trace!("FT_SetDtr({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_SetDtr(self.handle()) };
        ft_result((), status)
    }

    /// Clear the Data Terminal Ready (DTR) control signal.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.clear_dtr()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn clear_dtr(&mut self) -> Result<(), FtStatus> {
        trace!("FT_ClrtDtr({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_ClrDtr(self.handle()) };
        ft_result((), status)
    }

    /// Set the Request to Send (RTS) control signal.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_rts()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_rts(&mut self) -> Result<(), FtStatus> {
        trace!("FT_SetRts({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_SetRts(self.handle()) };
        ft_result((), status)
    }

    /// Clear the Request to Send (RTS) control signal.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.clear_rts()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn clear_rts(&mut self) -> Result<(), FtStatus> {
        trace!("FT_ClrtRts({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_ClrRts(self.handle()) };
        ft_result((), status)
    }

    /// Enables different chip modes.
    ///
    /// # Arguments
    ///
    /// * `mask` - This bit mask sets up which bits are inputs and outputs.
    ///   A bit value of 0 sets the corresponding pin to an input,
    ///   a bit value of 1 sets the corresponding pin to an output.
    ///   In the case of CBUS Bit Bang, the upper nibble of this value controls
    ///   which pins are inputs and outputs, while the lower nibble controls
    ///   which of the outputs are high and low.
    /// * `mode` - Bitmode, see the [`BitMode`] struct for more details.
    ///
    /// For a description of available bit modes for the FT232R,
    /// see the application note [Bit Bang Modes for the FT232R and FT245R].
    ///
    /// For a description of available bit modes for the FT2232,
    /// see the application note [Bit Mode Functions for the FT2232].
    ///
    /// For a description of Bit Bang Mode for the FT232B and FT245B,
    /// see the application note [FT232B/FT245B Bit Bang Mode].
    ///
    /// Application notes are available for download from the [FTDI website].
    ///
    /// Note that to use CBUS Bit Bang for the FT232R,
    /// the CBUS must be configured for CBUS Bit Bang in the EEPROM.
    ///
    /// Note that to use Single Channel Synchronous 245 FIFO mode for the
    /// FT2232H, channel A must be configured for FT245 FIFO mode in the EEPROM.
    ///
    /// [Bit Bang Modes for the FT232R and FT245R]: https://www.ftdichip.com/Support/Documents/AppNotes/AN_232R-01_Bit_Bang_Mode_Available_For_FT232R_and_Ft245R.pdf
    /// [Bit Mode Functions for the FT2232]: https://www.ftdichip.com/Support/Documents/AppNotes/AN2232C-02_FT2232CBitMode.pdf
    /// [FT232B/FT245B Bit Bang Mode]: https://www.ftdichip.com/Support/Documents/AppNotes/AN232B-01_BitBang.pdf
    /// [FTDI website]: https://www.ftdichip.com/Support/Documents/AppNotes.htm
    /// [`BitMode`]: ./enum.BitMode.html
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{BitMode, Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_bit_mode(0xFF, BitMode::AsyncBitbang)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_bit_mode(&mut self, mask: u8, mode: BitMode) -> Result<(), FtStatus> {
        trace!(
            "FT_SetBitMode({:?}, 0x{:02X}, 0x{:02X})",
            self.handle(),
            mask,
            mode as u8
        );
        let status: FT_STATUS = unsafe { FT_SetBitMode(self.handle(), mask, mode as u8) };

        ft_result((), status)
    }

    /// Get the instantaneous value of the data bus.
    ///
    /// **Note:** This is not the [`BitMode`] as set in [`set_bit_mode`],
    /// this is the value (logic high or logic low) of the pins on the bus.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let bitmode = ft.bit_mode()?;
    /// println!("Data bus state: {}", bitmode);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`set_bit_mode`]: #method.set_bit_mode
    /// [`BitMode`]: ./enum.BitMode.html
    fn bit_mode(&mut self) -> Result<u8, FtStatus> {
        let mut mode: u8 = 0;
        trace!("FT_GetBitMode({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_GetBitMode(self.handle(), &mut mode) };
        ft_result(mode, status)
    }

    /// Sets the BREAK condition for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_break_off()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_break_on(&mut self) -> Result<(), FtStatus> {
        trace!("FT_SetBreakOn({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_SetBreakOn(self.handle()) };
        ft_result((), status)
    }

    /// Resets the BREAK condition for the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.set_break_off()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn set_break_off(&mut self) -> Result<(), FtStatus> {
        trace!("FT_SetBreakOff({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_SetBreakOff(self.handle()) };
        ft_result((), status)
    }

    /// Gets the number of bytes in the receive queue.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut buf: [u8; 4096] = [0; 4096];
    /// let mut ft = Ftdi::new()?;
    /// let rx_bytes = ft.queue_status()?;
    ///
    /// if (rx_bytes > 0) {
    ///     ft.read(&mut buf[0..rx_bytes])?;
    /// }
    /// # Ok::<(), libftd2xx::TimeoutError>(())
    /// ```
    fn queue_status(&mut self) -> Result<usize, FtStatus> {
        let mut queue_status: u32 = 0;
        trace!("FT_GetQueueStatus({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_GetQueueStatus(self.handle(), &mut queue_status) };

        ft_result(usize::try_from(queue_status).unwrap(), status)
    }

    /// Gets the device status including number of characters in the receive
    /// queue, number of characters in the transmit queue, and the current event
    /// status.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let status = ft.status()?;
    /// println!("status={:?}", status);
    /// # Ok::<(), libftd2xx::TimeoutError>(())
    /// ```
    fn status(&mut self) -> Result<DeviceStatus, FtStatus> {
        let mut ammount_in_rx_queue: u32 = 0;
        let mut ammount_in_tx_queue: u32 = 0;
        let mut event_status: u32 = 0;

        trace!("FT_GetStatus({:?}, _, _, _)", self.handle());
        let status: FT_STATUS = unsafe {
            FT_GetStatus(
                self.handle(),
                &mut ammount_in_rx_queue,
                &mut ammount_in_tx_queue,
                &mut event_status,
            )
        };

        ft_result(
            DeviceStatus {
                ammount_in_rx_queue,
                ammount_in_tx_queue,
                event_status,
            },
            status,
        )
    }

    /// Read data from the device, returning the number of bytes read.
    ///
    /// See [`read_all`] for more information about reading from the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// const BUF_SIZE: usize = 256;
    /// let mut buf: [u8; BUF_SIZE] = [0; BUF_SIZE];
    /// let mut ft = Ftdi::new()?;
    /// let bytes_read: usize = ft.read(&mut buf)?;
    /// assert_eq!(bytes_read, BUF_SIZE);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`read_all`]: #method.read_all
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, FtStatus> {
        let mut bytes_returned: u32 = 0;
        let len: u32 = u32::try_from(buf.len()).unwrap();
        trace!("FT_Read({:?}, _, {}, _)", self.handle(), len);
        let status: FT_STATUS = unsafe {
            FT_Read(
                self.handle(),
                buf.as_mut_ptr() as *mut c_void,
                len,
                &mut bytes_returned,
            )
        };
        ft_result(usize::try_from(bytes_returned).unwrap(), status)
    }

    /// Read data from the device.
    ///
    /// This method does not return until the buffer has been filled, if no
    /// timeout has been set.
    /// The number of bytes in the receive queue can be determined by calling
    /// [`queue_status`], and then an buffer equal to the length of that
    /// value can be passed to [`read_all`] so that the function reads the
    /// device and returns immediately.
    ///
    /// When a read timeout value has been specified in a previous call to
    /// [`set_timeouts`], [`read_all`] returns when the timer expires or when
    /// the buffer has been filled, whichever occurs first.
    /// If the timeout occurred, [`read_all`] reads available data into the
    /// buffer and returns the [`TimeoutError`] error.
    ///
    /// # Examples
    ///
    /// ## Read all available data
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut buf: [u8; 4096] = [0; 4096];
    /// let mut ft = Ftdi::new()?;
    /// let rx_bytes = ft.queue_status()?;
    ///
    /// if rx_bytes > 0 {
    ///     ft.read_all(&mut buf[0..rx_bytes])?;
    /// }
    /// # Ok::<(), libftd2xx::TimeoutError>(())
    /// ```
    ///
    /// ## Read with a timeout of 5 seconds
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon, TimeoutError};
    /// use std::time::Duration;
    ///
    /// const BUF_LEN: usize = 4096;
    /// let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
    /// let mut ft = Ftdi::new()?;
    ///
    /// ft.set_timeouts(Duration::from_millis(5000), Duration::from_millis(0))?;
    ///
    /// let valid_data = match ft.read_all(&mut buf) {
    ///     Err(e) => match e {
    ///         TimeoutError::Timeout {
    ///             actual: actual,
    ///             expected: expected,
    ///         } => {
    ///             eprintln!("Read timeout occured after 5s! {:?}", e);
    ///             &buf[0..actual]
    ///         }
    ///         TimeoutError::FtStatus(status) => {
    ///             panic!("FTDI Status Error: {:?}", status);
    ///         }
    ///     },
    ///     _ => &buf[0..buf.len()],
    /// };
    /// # Ok::<(), libftd2xx::TimeoutError>(())
    /// ```
    ///
    /// [`read_all`]: #method.read_all
    /// [`queue_status`]: #method.queue_status
    /// [`set_timeouts`]: #method.set_timeouts
    /// [`TimeoutError`]: ./enum.TimeoutError.html
    fn read_all(&mut self, buf: &mut [u8]) -> Result<(), TimeoutError> {
        let num_read = self.read(buf)?;
        if num_read != buf.len() {
            Err(TimeoutError::Timeout {
                expected: buf.len(),
                actual: num_read,
            })
        } else {
            Ok(())
        }
    }

    /// Write data to the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// const BUF_SIZE: usize = 256;
    /// let buf: [u8; BUF_SIZE] = [0; BUF_SIZE];
    /// let mut ft = Ftdi::new()?;
    /// ft.write_all(&buf)?;
    /// # Ok::<(), libftd2xx::TimeoutError>(())
    /// ```
    fn write_all(&mut self, buf: &[u8]) -> Result<(), TimeoutError> {
        let num_written = self.write(buf)?;
        if num_written != buf.len() {
            Err(TimeoutError::Timeout {
                expected: buf.len(),
                actual: num_written,
            })
        } else {
            Ok(())
        }
    }

    /// Write data to the device, returning how many bytes were written.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// const BUF_SIZE: usize = 256;
    /// let buf: [u8; BUF_SIZE] = [0; BUF_SIZE];
    /// let mut ft = Ftdi::new()?;
    /// let bytes_written: usize = ft.write(&buf)?;
    /// assert_eq!(bytes_written, BUF_SIZE);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn write(&mut self, buf: &[u8]) -> Result<usize, FtStatus> {
        let mut bytes_written: u32 = 0;
        let len: u32 = u32::try_from(buf.len()).unwrap();
        trace!("FT_Write({:?}, _, {}, _)", self.handle(), len);
        let status: FT_STATUS = unsafe {
            FT_Write(
                self.handle(),
                buf.as_ptr() as *mut c_void,
                len,
                &mut bytes_written,
            )
        };
        ft_result(usize::try_from(bytes_written).unwrap(), status)
    }

    /// This function purges the transmit buffers in the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.purge_tx()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn purge_tx(&mut self) -> Result<(), FtStatus> {
        trace!("FT_Purge({:?}, {})", self.handle(), FT_PURGE_TX);
        let status: FT_STATUS = unsafe { FT_Purge(self.handle(), FT_PURGE_TX) };
        ft_result((), status)
    }

    /// This function purges the receive buffers in the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.purge_rx()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn purge_rx(&mut self) -> Result<(), FtStatus> {
        trace!("FT_Purge({:?}, {})", self.handle(), FT_PURGE_RX);
        let status: FT_STATUS = unsafe { FT_Purge(self.handle(), FT_PURGE_RX) };
        ft_result((), status)
    }

    /// This function purges the transmit and receive buffers in the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.purge_all()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn purge_all(&mut self) -> Result<(), FtStatus> {
        trace!(
            "FT_Purge({:?}, {})",
            self.handle(),
            FT_PURGE_TX | FT_PURGE_RX
        );
        let status: FT_STATUS = unsafe { FT_Purge(self.handle(), FT_PURGE_TX | FT_PURGE_RX) };
        ft_result((), status)
    }

    /// Close an open device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.close()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn close(&mut self) -> Result<(), FtStatus> {
        trace!("FT_Close({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_Close(self.handle()) };
        ft_result((), status)
    }

    /// Get the COM port associated with a device.
    ///
    /// This method is only available when using the Windows CDM driver as both
    /// the D2XX and VCP drivers can be installed at the same time.
    ///
    /// If no COM port is associated with the device `None` is returned.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// match ft.com_port_number()? {
    ///     Some(num) => println!("COM{}", num),
    ///     None => println!("no COM port"),
    /// }
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    #[cfg(all(any(windows, doc), not(doctest)))]
    #[cfg_attr(docsrs, doc(cfg(windows)))]
    fn com_port_number(&mut self) -> Result<Option<u32>, FtStatus> {
        let mut num: i32 = -1;
        trace!("FT_GetComPortNumber({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_GetComPortNumber(self.handle(), &mut num as *mut i32) };
        ft_result(
            if num == -1 {
                None
            } else {
                Some(u32::try_from(num).unwrap())
            },
            status,
        )
    }

    /// Send a reset command to the port.
    ///
    /// This method is available on Windows only.
    ///
    /// This function is used to attempt to recover the device upon failure.
    /// For the equivalent of a unplug-replug event use
    /// [`FtdiCommon::cycle_port`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.reset_port()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    #[cfg(all(any(windows, doc), not(doctest)))]
    #[cfg_attr(docsrs, doc(cfg(windows)))]
    fn reset_port(&mut self) -> Result<(), FtStatus> {
        trace!("FT_ResetPort({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_ResetPort(self.handle()) };
        ft_result((), status)
    }

    /// Send a cycle command to the USB port.
    ///
    /// This method is available on Windows only.
    ///
    /// The effect of this method is the same as disconnecting then
    /// reconnecting the device from the USB port.
    /// Possible use of this method is situations where a fatal error has
    /// occurred and it is difficult, or not possible, to recover without
    /// unplugging and replugging the USB cable.
    /// This method can also be used after re-programming the EEPROM to force
    /// the FTDI device to read the new EEPROM contents which would otherwise
    /// require a physical disconnect-reconnect.
    ///
    /// As the current session is not restored when the driver is reloaded,
    /// the application must be able to recover after calling this method.
    /// It is the responsibility of the application to close the handle after
    /// successfully calling this method.
    ///
    /// For FT4232H, FT2232H and FT2232 devices, `cycle_port` will only work
    /// under Windows XP and later.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.cycle_port()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    #[cfg(all(any(windows, doc), not(doctest)))]
    #[cfg_attr(docsrs, doc(cfg(windows)))]
    fn cycle_port(&mut self) -> Result<(), FtStatus> {
        trace!("FT_CyclePort({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_CyclePort(self.handle()) };
        ft_result((), status)
    }

    /// Gets the modem status and line status from the device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let modem_status = ft.modem_status()?;
    /// println!("CTS={}", modem_status.clear_to_send());
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn modem_status(&mut self) -> Result<ModemStatus, FtStatus> {
        let mut modem_status: u32 = 0;
        trace!("FT_GetModemStatus({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_GetModemStatus(self.handle(), &mut modem_status) };
        ft_result(ModemStatus::new(modem_status), status)
    }

    /// Read a value from an EEPROM location.
    ///
    /// # Arguments
    ///
    /// * `offset` - EEPROM location to read from.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// const LOCATION: u32 = 0x0;
    /// let mut ft = Ftdi::new()?;
    /// let value = ft.eeprom_word_read(LOCATION)?;
    /// println!(
    ///     "The value at EEPROM address 0x{:X} is 0x{:04X}",
    ///     LOCATION, value
    /// );
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn eeprom_word_read(&mut self, offset: u32) -> Result<u16, FtStatus> {
        let mut value: u16 = 0;
        trace!("FT_ReadEE({:?}, {}, _)", self.handle(), offset);
        let status: FT_STATUS = unsafe { FT_ReadEE(self.handle(), offset, &mut value) };
        ft_result(value, status)
    }

    /// Writes a value to an EEPROM location.
    ///
    /// # Arguments
    ///
    /// * `offset` - EEPROM location to write to.
    /// * `value` - Value to write.
    ///
    /// # Warning
    ///
    /// Writing bad values to the EEPROM can brick your device.
    /// Please take a moment to read the license for this crate before using
    /// this function.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.eeprom_word_write(0x0, 0x1234)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn eeprom_word_write(&mut self, offset: u32, value: u16) -> Result<(), FtStatus> {
        trace!(
            "FT_WriteEE({:?}, 0x{:X}, 0x{:04X})",
            self.handle(),
            offset,
            value
        );
        let status: FT_STATUS = unsafe { FT_WriteEE(self.handle(), offset, value) };
        ft_result((), status)
    }

    /// Erases the entire contents of the EEPROM, including the user area.
    ///
    /// **Note:** The FT232R and FT245R have an internal EEPROM that cannot be
    /// erased.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// ft.eeprom_erase()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn eeprom_erase(&mut self) -> Result<(), FtStatus> {
        trace!("FT_EraseEE({:?})", self.handle());
        let status: FT_STATUS = unsafe { FT_EraseEE(self.handle()) };
        ft_result((), status)
    }

    /// Get the available size of the EEPROM user area in bytes.
    ///
    /// The user area of an FTDI device EEPROM is the total area of the EEPROM
    /// that is unused by device configuration information and descriptors.
    /// This area is available to the user to store information specific to
    /// their application.
    /// The size of the user area depends on the length of the Manufacturer,
    /// ManufacturerId, Description and SerialNumber strings programmed into the
    /// EEPROM.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let ua_size = ft.eeprom_user_size()?;
    /// println!("EEPROM user area size: {} Bytes", ua_size);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    fn eeprom_user_size(&mut self) -> Result<usize, FtStatus> {
        let mut value: u32 = 0;
        trace!("FT_EE_UASize({:?}, _)", self.handle());
        let status: FT_STATUS = unsafe { FT_EE_UASize(self.handle(), &mut value) };
        ft_result(usize::try_from(value).unwrap(), status)
    }

    /// Read the contents of the EEPROM user area.
    ///
    /// The size of the user area can be determined with [`eeprom_user_size`].
    ///
    /// The function returns the actual number of bytes read into the buffer.
    /// If the buffer is larger than the user size the return value will be less
    /// than the length of the buffer.
    /// The return value should be checked to ensure the expected number of
    /// bytes was read.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let mut buf: [u8; 9] = [0; 9];
    /// let num_read = ft.eeprom_user_read(&mut buf)?;
    /// assert_eq!(buf.len(), num_read);
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`eeprom_user_size`]: #method.eeprom_user_size
    fn eeprom_user_read(&mut self, buf: &mut [u8]) -> Result<usize, FtStatus> {
        let mut num_read: u32 = 0;
        let len: u32 = u32::try_from(buf.len()).unwrap();
        trace!("FT_EE_UARead({:?}, _, {}, _)", self.handle(), len);
        let status: FT_STATUS =
            unsafe { FT_EE_UARead(self.handle(), buf.as_mut_ptr(), len, &mut num_read) };
        ft_result(usize::try_from(num_read).unwrap(), status)
    }

    /// Write to the EEPROM user area.
    ///
    /// An error will be returned when the buffer size is larger than the user
    /// area size.
    /// The size of the user area can be determined with [`eeprom_user_size`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::{Ftdi, FtdiCommon};
    ///
    /// let mut ft = Ftdi::new()?;
    /// let data = "Hello, World";
    /// ft.eeprom_user_write(&data.as_bytes())?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`eeprom_user_size`]: #method.eeprom_user_size
    fn eeprom_user_write(&mut self, buf: &[u8]) -> Result<(), FtStatus> {
        let len: u32 = u32::try_from(buf.len()).unwrap();
        trace!("FT_EE_UAWrite({:?}, _, {})", self.handle(), len);
        let status: FT_STATUS =
            unsafe { FT_EE_UAWrite(self.handle(), buf.as_ptr() as *mut u8, len) };
        ft_result((), status)
    }
}

/// FTDI device-specific EEPROM trait.
pub trait FtdiEeprom<
    T: Default + std::fmt::Debug + std::convert::From<Eeprom>,
    Eeprom: Default + std::fmt::Debug + std::convert::From<T>,
>: FtdiCommon
{
    /// Read from the FTD2XX device EEPROM.
    ///
    /// # Example
    ///
    /// This example uses the FT232H.
    ///
    /// ```no_run
    /// use libftd2xx::{Ft4232h, Ftdi, FtdiEeprom};
    /// use std::convert::TryFrom;
    ///
    /// let mut ftdi = Ftdi::new()?;
    /// let mut ft = Ft4232h::try_from(&mut ftdi)?;
    /// let (eeprom, eeprom_strings) = ft.eeprom_read()?;
    /// println!("FT4232H EEPROM contents: {:?}", eeprom);
    /// println!("FT4232H EEPROM strings: {:?}", eeprom_strings);
    /// # Ok::<(), libftd2xx::DeviceTypeError>(())
    /// ```
    fn eeprom_read(&mut self) -> Result<(Eeprom, EepromStrings), FtStatus> {
        let mut manufacturer: [i8; STRING_LEN] = [0; STRING_LEN];
        let mut manufacturer_id: [i8; STRING_LEN] = [0; STRING_LEN];
        let mut description: [i8; STRING_LEN] = [0; STRING_LEN];
        let mut serial_number: [i8; STRING_LEN] = [0; STRING_LEN];

        let mut eeprom_data: T = Eeprom::default().into();
        let eeprom_data_size = u32::try_from(mem::size_of::<T>()).unwrap();

        trace!(
            "FT_EEPROM_Read({:?}, _, {}, _, _, _, _)",
            self.handle(),
            eeprom_data_size
        );
        let status: FT_STATUS = unsafe {
            FT_EEPROM_Read(
                self.handle(),
                &mut eeprom_data as *mut T as *mut c_void,
                eeprom_data_size,
                manufacturer.as_mut_ptr() as *mut c_char,
                manufacturer_id.as_mut_ptr() as *mut c_char,
                description.as_mut_ptr() as *mut c_char,
                serial_number.as_mut_ptr() as *mut c_char,
            )
        };

        ft_result(
            (
                eeprom_data.into(),
                EepromStrings::with_slices(
                    &manufacturer,
                    &manufacturer_id,
                    &description,
                    &serial_number,
                )
                // safe to unwrap since driver cannot return invalid strings
                // in this case
                .unwrap(),
            ),
            status,
        )
    }

    /// Program the FTD2XX EEPROM.
    ///
    /// # Warning
    ///
    /// Writing bad values to the EEPROM can brick your device.
    /// Please take a moment to read the license for this crate before using
    /// this function.
    ///
    /// # Example
    ///
    /// This example uses the FT232H.
    ///
    /// ```no_run
    /// use libftd2xx::{DriverType, Eeprom232h, EepromStrings, Ft232h, Ftdi, FtdiEeprom};
    ///
    /// let mut ft = Ft232h::with_serial_number("FT4PWSEOA")?;
    /// let strings = EepromStrings::with_strs("FTDI", "FT", "Hello World", "FT1234567")?;
    /// let mut eeprom = Eeprom232h::default();
    /// eeprom.set_driver_type(DriverType::D2XX);
    /// ft.eeprom_program(eeprom, strings)?;
    /// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
    /// ```
    fn eeprom_program(&mut self, eeprom: Eeprom, strings: EepromStrings) -> Result<(), FtStatus> {
        let manufacturer = std::ffi::CString::new(strings.manufacturer()).unwrap();
        let manufacturer_id = std::ffi::CString::new(strings.manufacturer_id()).unwrap();
        let description = std::ffi::CString::new(strings.description()).unwrap();
        let serial_number = std::ffi::CString::new(strings.serial_number()).unwrap();
        let mut eeprom_data: T = eeprom.into();
        let eeprom_data_size = u32::try_from(mem::size_of::<T>()).unwrap();

        trace!(
            r#"FT_EEPROM_Program({:?}, {:?}, {}, "{}", "{}", "{}", "{}")"#,
            self.handle(),
            eeprom_data,
            eeprom_data_size,
            strings.manufacturer(),
            strings.manufacturer_id(),
            strings.description(),
            strings.serial_number(),
        );
        let status: FT_STATUS = unsafe {
            FT_EEPROM_Program(
                self.handle(),
                &mut eeprom_data as *mut T as *mut c_void,
                eeprom_data_size,
                manufacturer.as_ptr() as *mut c_char,
                manufacturer_id.as_ptr() as *mut c_char,
                description.as_ptr() as *mut c_char,
                serial_number.as_ptr() as *mut c_char,
            )
        };

        ft_result((), status)
    }
}

fn ft_open_ex(arg: &str, flag: u32) -> Result<FT_HANDLE, FtStatus> {
    let mut handle: FT_HANDLE = std::ptr::null_mut();
    let cstr_arg = std::ffi::CString::new(arg).unwrap();
    trace!(r#"FT_OpenEx("{}", {}, _)"#, arg, flag);
    let status: FT_STATUS =
        unsafe { FT_OpenEx(cstr_arg.as_ptr() as *mut c_void, flag, &mut handle) };
    ft_result(handle, status)
}

impl Ftdi {
    /// Open the first device on the system.
    ///
    /// This is equivalent to calling [`with_index`] with an index of `0`.
    ///
    /// This function cannot be used to open a specific device.
    /// Ordering of devices on a system is not guaranteed to remain constant.
    /// Calling this function multiple times may result in a different device
    /// each time when there is more than one device connected to the system.
    /// Use [`with_serial_number`] to open a specific device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ftdi;
    ///
    /// Ftdi::new()?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`with_index`]: #method.with_index
    /// [`with_serial_number`]: #method.with_serial_number
    pub fn new() -> Result<Ftdi, FtStatus> {
        Ftdi::with_index(0)
    }

    /// Open the device by an arbitrary index and initialize the handle.
    ///
    /// This function can open multiple devices, but it cannot be used to open
    /// a specific device.
    /// Ordering of devices on a system is not guaranteed to remain constant.
    /// Calling this function multiple times with the same index may result in a
    /// different device each time when there is more than one device connected
    /// to the system.
    /// Use [`with_serial_number`] to open a specific device.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ftdi;
    ///
    /// Ftdi::with_index(0)?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    ///
    /// [`with_serial_number`]: #method.with_serial_number
    pub fn with_index(index: i32) -> Result<Ftdi, FtStatus> {
        let mut handle: FT_HANDLE = std::ptr::null_mut();
        trace!("FT_Open({}, _)", index);
        let status: FT_STATUS = unsafe { FT_Open(index, &mut handle) };
        ft_result(Ftdi { handle }, status)
    }

    /// Open the device by its serial number and initialize the handle.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ftdi;
    ///
    /// Ftdi::with_serial_number("FT59UO4C")?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    pub fn with_serial_number(serial_number: &str) -> Result<Ftdi, FtStatus> {
        let handle = ft_open_ex(serial_number, FT_OPEN_BY_SERIAL_NUMBER)?;
        Ok(Ftdi { handle })
    }

    /// Open the device by its device description.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ftdi;
    ///
    /// Ftdi::with_description("Hello")?;
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    pub fn with_description(description: &str) -> Result<Ftdi, FtStatus> {
        let handle = ft_open_ex(description, FT_OPEN_BY_DESCRIPTION)?;
        Ok(Ftdi { handle })
    }
}

impl Ft232h {
    /// Open a `Ft4232h` device and initialize the handle.
    ///
    /// # Safety
    ///
    /// This is **unchecked** meaning a device type check will not be performed.
    /// Methods that require this specific device type may fail in unexpected
    /// ways if the wrong device is used.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ft232h;
    ///
    /// let mut ft = unsafe { Ft232h::with_serial_number_unchecked("FT5AVX6B")? };
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    pub unsafe fn with_serial_number_unchecked(serial_number: &str) -> Result<Ft232h, FtStatus> {
        let handle = ft_open_ex(serial_number, FT_OPEN_BY_SERIAL_NUMBER)?;
        Ok(Ft232h { handle })
    }

    /// Open a `Ft232h` device and initialize the handle.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ft232h;
    ///
    /// Ft232h::with_serial_number("FT59UO4C")?;
    /// # Ok::<(), libftd2xx::DeviceTypeError>(())
    /// ```
    pub fn with_serial_number(serial_number: &str) -> Result<Ft232h, DeviceTypeError> {
        let mut unknown = Ftdi::with_serial_number(serial_number)?;
        Ft232h::try_from(&mut unknown)
    }

    /// Open a `Ft232h` device by its device description.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ft232h;
    ///
    /// Ft232h::with_description("Hello")?;
    /// # Ok::<(), libftd2xx::DeviceTypeError>(())
    /// ```
    pub fn with_description(serial_number: &str) -> Result<Ft232h, DeviceTypeError> {
        let mut unknown = Ftdi::with_description(serial_number)?;
        Ft232h::try_from(&mut unknown)
    }
}

impl Ft4232h {
    /// Open a `Ft4232h` device and initialize the handle.
    ///
    /// # Safety
    ///
    /// This is **unchecked** meaning a device type check will not be performed.
    /// Methods that require this specific device type may fail in unexpected
    /// ways if the wrong device is used.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ft4232h;
    ///
    /// let mut ft = unsafe { Ft4232h::with_serial_number_unchecked("FT4PWSEOA")? };
    /// # Ok::<(), libftd2xx::FtStatus>(())
    /// ```
    pub unsafe fn with_serial_number_unchecked(serial_number: &str) -> Result<Ft4232h, FtStatus> {
        let handle = ft_open_ex(serial_number, FT_OPEN_BY_SERIAL_NUMBER)?;
        Ok(Ft4232h { handle })
    }

    /// Open a `Ft4232h` device and initialize the handle.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ft4232h;
    ///
    /// Ft4232h::with_serial_number("FT4PWSEOA")?;
    /// # Ok::<(), libftd2xx::DeviceTypeError>(())
    /// ```
    pub fn with_serial_number(serial_number: &str) -> Result<Ft4232h, DeviceTypeError> {
        let mut unknown = Ftdi::with_serial_number(serial_number)?;
        Ft4232h::try_from(&mut unknown)
    }

    /// Open a `Ft4232h` device by its device description.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use libftd2xx::Ft4232h;
    ///
    /// Ft4232h::with_description("FT4232H-56Q MiniModule A")?;
    /// # Ok::<(), libftd2xx::DeviceTypeError>(())
    /// ```
    pub fn with_description(serial_number: &str) -> Result<Ft4232h, DeviceTypeError> {
        let mut unknown = Ftdi::with_description(serial_number)?;
        Ft4232h::try_from(&mut unknown)
    }
}

macro_rules! impl_boilerplate_for {
    ($DEVICE:ident, $TYPE:expr) => {
        impl FtdiCommon for $DEVICE {
            const DEVICE_TYPE: DeviceType = $TYPE;

            fn handle(&mut self) -> FT_HANDLE {
                self.handle
            }
        }
    };
}

macro_rules! impl_try_from_for {
    ($DEVICE:ident) => {
        impl TryFrom<&mut Ftdi> for $DEVICE {
            type Error = DeviceTypeError;

            fn try_from(ft: &mut Ftdi) -> Result<Self, Self::Error> {
                let device_type: DeviceType = ft.device_info()?.device_type;
                if device_type != Self::DEVICE_TYPE {
                    Err(DeviceTypeError::DeviceType {
                        expected: $DEVICE::DEVICE_TYPE,
                        detected: device_type,
                    })
                } else {
                    Ok($DEVICE {
                        handle: ft.handle(),
                    })
                }
            }
        }
    };
}

impl_boilerplate_for!(Ftdi, DeviceType::Unknown);
impl_boilerplate_for!(Ft232h, DeviceType::FT232H);
impl_boilerplate_for!(Ft4232h, DeviceType::FT4232H);

impl_try_from_for!(Ft232h);
impl_try_from_for!(Ft4232h);

impl FtdiEeprom<FT_EEPROM_232H, Eeprom232h> for Ft232h {}
impl FtdiEeprom<FT_EEPROM_4232H, Eeprom4232h> for Ft4232h {}

impl FtdiMpsse for Ft232h {}
impl FtdiMpsse for Ft4232h {}
impl Ftx232hMpsse for Ft232h {}
impl Ftx232hMpsse for Ft4232h {}