wincwifi 0.3.0

WINC1500 Wifi chip embedded driver
Documentation
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
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::StackError;

use arrayvec::ArrayString;

use super::constants::{
    AuthType, WifiChannel, MAX_HOST_NAME_LEN, MAX_PSK_KEY_LEN, MAX_S802_PASSWORD_LEN,
    MAX_S802_USERNAME_LEN, MAX_SSID_LEN, MAX_WEP_KEY_LEN, MIN_PSK_KEY_LEN,
};

#[cfg(feature = "experimental-ecc")]
use super::constants::{EccCurveType, EccRequestType};

#[cfg(feature = "wep")]
use super::constants::{WepKeyIndex, MIN_WEP_KEY_LEN};
use core::net::Ipv4Addr;

/// Default IP address "192.168.1.1" for access point and provisioning mode.
const DEFAULT_AP_IP: u32 = 0xC0A80101;
#[cfg(feature = "experimental-ecc")]
const ECC_POINT_MAX_SIZE: usize = 32;
/// Maximum number of octets in Mac Address
const MAX_OCTETS_IN_MAC_ADDRESS: usize = 6;

/// Device Domain name.
pub type HostName = ArrayString<MAX_HOST_NAME_LEN>;
/// Wifi SSID
pub type Ssid = ArrayString<MAX_SSID_LEN>;
/// WPA-PSK key
pub type WpaKey = ArrayString<MAX_PSK_KEY_LEN>;
/// Wep Key
pub type WepKey = ArrayString<MAX_WEP_KEY_LEN>;
/// S802_1X Username
pub type S8Username = ArrayString<MAX_S802_USERNAME_LEN>;
/// S802_1X Password
pub type S8Password = ArrayString<MAX_S802_PASSWORD_LEN>;
/// ECDSA Verify Information.
#[cfg(feature = "experimental-ecc")]
pub(crate) type EcdsaVerifyInfo = u32;

/// Wi-Fi Security Credentials.
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Credentials {
    Open = 1,
    /// WPA-PSK Passphrase: Must be at least 8 bytes (MIN) and no more than 63 bytes long.
    WpaPSK(WpaKey) = 2,
    /// Wep Passphrase: Should be 10 bytes for 40-bit and 26 bytes for 104-bit.
    /// Wep key Index: Between 1 and 4.
    #[cfg(feature = "wep")]
    Wep(WepKey, WepKeyIndex) = 3,
    /// 802.1X Username: Should not be greater then 20 bytes.
    /// 802.1X Password: Should not be greater then 40 bytes.
    S802_1X(S8Username, S8Password) = 4,
}

/// Socket Options
#[derive(PartialEq, Eq)]
pub enum SocketOptions {
    Tcp(TcpSockOpts),
    Udp(UdpSockOpts),
}

/// Socket Options
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum UdpSockOpts {
    /// Receive Timeout
    ReceiveTimeout(u32) = 0xff,
    /// Enable/Disable callback for UDP send.
    SetUdpSendCallback(bool) = 0x00,
    /// Join Multicast group
    JoinMulticast(Ipv4Addr) = 0x01,
    /// Leave Multicast group
    LeaveMulticast(Ipv4Addr) = 0x02,
}

/// TCP Socket Options
#[repr(u8)]
#[derive(PartialEq, Eq)]
pub enum TcpSockOpts {
    /// Receive Timeout
    ReceiveTimeout(u32) = 0xff,
    /// SSL Socket Options
    #[cfg(feature = "ssl")]
    Ssl(SslSockOpts) = 0xfe,
}

/// TLS Socket Option
#[cfg(feature = "ssl")]
#[repr(u8)]
#[derive(PartialEq, Eq)]
// Note: The `Copy` trait is not implemented to avoid
// copying the `ArrayString` (HostName).
pub enum SslSockOpts {
    /// SSL Configuration Options
    Config(SslSockConfig, bool),
    /// Server Name Indication (SNI).
    SetSni(HostName) = 0x02,
}

/// TLS Socket Configuration
#[cfg(feature = "ssl")]
#[repr(u8)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum SslSockConfig {
    /// Enable SSL.
    EnableSSL = 0x21, // 0x01 | 0x20 (SSL_FLAGS_NO_TX_COPY)
    /// Bypass X.509 certificate verification.
    /// Not recommended for production environments.
    BypassX509Verification = 0x02,
    /// Enable session caching to allow TLS
    /// session resumption for faster future connections.
    EnableSessionCache = 0x10,
    /// Enable SNI validation against the server's
    /// certificate subject common name.
    EnableSniValidation = 0x40,
}

/// Structure for Provisioning Information.
pub struct ProvisioningInfo {
    /// The SSID (network name) of the network.
    pub ssid: Ssid,
    /// Credentials for network's security.
    pub key: Credentials,
    // Status of Provisioning.
    pub(crate) status: bool,
}

/// Structure for Access Point Configuration.
#[derive(Debug, PartialEq, Eq)]
pub struct AccessPoint<'a> {
    /// The SSID (network name) of the network.
    pub ssid: &'a Ssid,
    /// The passphrase (Wi-Fi key) for the network's security.
    pub key: Credentials,
    /// The channel number (1..14) or 255 for all channels used by the access point.
    pub channel: WifiChannel,
    /// Whether the SSID is hidden (true for hidden).
    pub ssid_hidden: bool,
    /// IP address for the access point. The last octet must be in the range 1 to 99,
    /// for example: 192.168.1.1 to 192.168.1.99.
    /// Invalid Ip: 192.168.1.0 or 192.168.1.100.
    pub ip: Ipv4Addr,
}

/// Elliptic Curve point representation.
#[cfg(feature = "experimental-ecc")]
#[derive(Default)]
pub struct EccPoint {
    /// The X-coordinate of the ecc point.
    pub x_pos: [u8; ECC_POINT_MAX_SIZE],
    /// The Y-coordinate of the ecc point.
    pub y_pos: [u8; ECC_POINT_MAX_SIZE],
    // Point size in bytes (for each of the coordinates).
    pub point_size: u16,
    /// ID for the corresponding private key.
    pub private_key_id: u16,
}

/// Elliptic Curve Diffie-Hellman (ECDH) information.
#[cfg(feature = "experimental-ecc")]
#[derive(Default)]
pub struct EcdhInfo {
    /// ECC public key.
    pub ecc_point: EccPoint,
    /// Pre master secret generated during operations of
    /// type `ECC_REQ_CLIENT_ECDH` and `ECC_REQ_SERVER_ECDH`.
    pub private_key: [u8; ECC_POINT_MAX_SIZE],
}

/// Elliptic Curve Digital Signature Algorithm (ECDSA) signing information.
#[cfg(feature = "experimental-ecc")]
#[derive(Default)]
pub struct EcdsaSignInfo {
    /// Key Curve Type (NIST P-256, P-384)
    pub curve_type: EccCurveType,
    /// Hash Size (32 bytes for SHA-256 or 48 bytes for SHA-384)
    pub hash_size: u16,
}

/// ECC Operations Info.
#[cfg(feature = "experimental-ecc")]
#[derive(Default)]
pub struct EccInfo {
    /// ECC Request type (ECDH, ECDSA Sign or ECDSA Verify)
    pub req: EccRequestType,
    /// Status of ECC operation.
    /// Value should be used in response and ignored in request.
    /// zero for success and non-zero for failure.
    pub status: u16,
    /// User data
    /// Value in response should match the value in request from WINC.
    pub user_data: u32,
    /// Sequence number
    /// Value in response to WINC should match the value in request from WINC.
    pub seq_num: u32,
}

/// ECC Request Information received from WINC.
#[cfg(feature = "experimental-ecc")]
#[derive(Default)]
pub(crate) struct EccRequest {
    pub hif_reg: u32,
    pub ecc_info: EccInfo,
    pub ecdh_info: Option<EcdhInfo>,
    pub ecdsa_sign_info: Option<EcdsaSignInfo>,
    pub ecdsa_verify_info: Option<EcdsaVerifyInfo>,
}

/// SSL callback info
#[cfg(feature = "ssl")]
#[derive(Default)]
pub(crate) struct SslCallbackInfo {
    pub(crate) cipher_suite_bitmap: Option<Option<u32>>,
    #[cfg(feature = "experimental-ecc")]
    pub(crate) ecc_req: Option<EccRequest>,
}

/// MAC Address
pub struct MacAddress {
    mac: [u8; MAX_OCTETS_IN_MAC_ADDRESS],
}

/// Ethernet RX information.
#[cfg(feature = "ethernet")]
pub(crate) struct EthernetRxInfo {
    pub(crate) packet_size: u16,
    pub(crate) data_offset: u16,
    pub(crate) hif_address: u32,
}

/// Implementation to convert the Credentials to Authentication Type
impl From<Credentials> for AuthType {
    fn from(cred: Credentials) -> Self {
        match cred {
            Credentials::Open => Self::Open,
            Credentials::WpaPSK(_) => Self::WpaPSK,
            #[cfg(feature = "wep")]
            Credentials::Wep(_, _) => Self::WEP,
            Credentials::S802_1X(_, _) => Self::S802_1X,
        }
    }
}

/// Implementation to convert the Credentials to `u8` value.
impl From<Credentials> for u8 {
    fn from(val: Credentials) -> Self {
        match val {
            Credentials::Open => 1,
            Credentials::WpaPSK(_) => 2,
            #[cfg(feature = "wep")]
            Credentials::Wep(_, _) => 3,
            Credentials::S802_1X(_, _) => 4,
        }
    }
}

/// Implementation of `Credentials` to create new configuration or get length of stored key.
impl Credentials {
    /// Get the length of password stored in the Credentials.
    pub fn key_len(&self) -> usize {
        match self {
            Credentials::Open => 0,
            #[cfg(feature = "wep")]
            Credentials::Wep(key, _) => key.len(),
            Credentials::WpaPSK(key) => key.len(),
            Credentials::S802_1X(_, key) => key.len(),
        }
    }

    /// Generates WPA-PSK credentials from a password.
    ///
    /// # Arguments
    ///
    /// * `password` - The WPA-PSK passphrase. Must be at least 8 bytes and no more than 63 bytes long.
    ///
    /// # Returns
    ///
    /// * `Credentials::WpaPSK` - Configured WPA-PSK credentials on success.
    /// * `StackError` - If any parameter validation fails.
    pub fn from_wpa(password: &str) -> Result<Self, StackError> {
        if password.len() < MIN_PSK_KEY_LEN {
            return Err(StackError::InvalidParameters);
        }

        WpaKey::from(password)
            .map(Self::WpaPSK)
            .map_err(|_| StackError::InvalidParameters)
    }

    /// Generates 802.1X credentials from a username and password.
    ///
    /// # Arguments
    ///
    /// * `username` - The RADIUS server username. Must not exceed 20 bytes.
    /// * `password` - The RADIUS server password. Must not exceed 40 bytes.
    ///
    /// # Returns
    ///
    /// * `Credentials::S802_1X` - Configured 802.1X credentials on success.
    /// * `StackError` - If any parameter validation fails.
    pub fn from_s802(username: &str, password: &str) -> Result<Self, StackError> {
        let username = S8Username::from(username).map_err(|_| StackError::InvalidParameters)?;
        let password = S8Password::from(password).map_err(|_| StackError::InvalidParameters)?;
        Ok(Self::S802_1X(username, password))
    }

    #[cfg(feature = "wep")]
    /// Generates WEP credentials from a WEP key and key index.
    ///
    /// # Arguments
    ///
    /// * `key` - The WEP key. Must be 10 bytes for 40-bit or 26 bytes for 104-bit.
    /// * `key_index` - The index of the WEP key to use.
    ///
    /// # Returns
    ///
    /// * `Ok(Credentials::WEP)` - Configured WEP credentials on success.
    /// * `Err(StackError)` - If parameter validation fails.
    pub fn from_wep(key: &str, key_index: WepKeyIndex) -> Result<Self, StackError> {
        if key.len() != MAX_WEP_KEY_LEN && key.len() != MIN_WEP_KEY_LEN {
            return Err(StackError::InvalidParameters);
        }

        WepKey::from(key)
            .map(|key| Credentials::Wep(key, key_index))
            .map_err(|_| StackError::InvalidParameters)
    }
}

/// Implementation to convert `UdpSockOpts` to `u8` value.
impl From<UdpSockOpts> for u8 {
    fn from(value: UdpSockOpts) -> Self {
        match value {
            UdpSockOpts::SetUdpSendCallback(_) => 0x00,
            UdpSockOpts::JoinMulticast(_) => 0x01,
            UdpSockOpts::LeaveMulticast(_) => 0x02,
            UdpSockOpts::ReceiveTimeout(_) => 0xff,
        }
    }
}

/// Implementation to get 32-bit value stored in UDP socket option.
impl UdpSockOpts {
    /// Get the value of the Socket option.
    pub fn get_value(&self) -> u32 {
        match self {
            UdpSockOpts::ReceiveTimeout(val) => *val,
            UdpSockOpts::SetUdpSendCallback(val) => *val as u32,
            UdpSockOpts::JoinMulticast(val) | UdpSockOpts::LeaveMulticast(val) => {
                // Address needs to be in big endian format.
                u32::from_le_bytes(val.to_bits().to_be_bytes())
            }
        }
    }
}

/// Implementation to convert `TcpSockOpts` to `u8` value.
impl From<TcpSockOpts> for u8 {
    fn from(value: TcpSockOpts) -> Self {
        match value {
            #[cfg(feature = "ssl")]
            TcpSockOpts::Ssl(_) => 0xfe,
            TcpSockOpts::ReceiveTimeout(_) => 0xff,
        }
    }
}

/// Implementation to get 32-bit value stored in TCP socket option.
impl TcpSockOpts {
    /// Get the value of the Socket option.
    pub fn get_value(&self) -> u32 {
        match self {
            TcpSockOpts::ReceiveTimeout(val) => *val,
            // SSL values don't have 32 bit values.
            #[cfg(feature = "ssl")]
            TcpSockOpts::Ssl(_) => 0xfe,
        }
    }
}

/// Implementation to convert `SslSockOpts` to `u8` value.
#[cfg(feature = "ssl")]
impl From<SslSockOpts> for u8 {
    fn from(value: SslSockOpts) -> Self {
        match value {
            SslSockOpts::Config(cfg, _) => cfg.into(),
            SslSockOpts::SetSni(_) => 0x02,
        }
    }
}

/// Implementation to convert `&SslSockOpts` to `u8` value.
#[cfg(feature = "ssl")]
impl From<&SslSockOpts> for u8 {
    fn from(value: &SslSockOpts) -> Self {
        match value {
            SslSockOpts::Config(cfg, _) => (*cfg).into(),
            SslSockOpts::SetSni(_) => 0x02,
        }
    }
}

/// Implementation to convert `SslSockConfig` to `u8` value.
#[cfg(feature = "ssl")]
impl From<SslSockConfig> for u8 {
    fn from(value: SslSockConfig) -> Self {
        value as Self
    }
}

/// Implementation to get value stored in SSL socket option.
#[cfg(feature = "ssl")]
impl SslSockOpts {
    /// Gets the SNI value passed in the `SslSockOpts::SetSni` socket option.
    ///
    /// # Returns
    ///
    /// * `HostName` – The stored hostname as a string array.
    /// * `StackError` – If the method is called for any SSL socket option other than `SetSni`.
    pub(crate) fn get_sni_value(&self) -> Result<&HostName, StackError> {
        match self {
            SslSockOpts::SetSni(hostname) => Ok(hostname),
            _ => Err(StackError::InvalidParameters),
        }
    }
}

/// Implementation to create Socket Option configuration
impl SocketOptions {
    /// Set the socket option to join an IPv4 multicast group.
    ///
    /// # Arguments
    ///
    /// * `addr` - The IPv4 multicast address to join.
    ///
    /// # Returns
    ///
    /// * `SocketOption::Udp(UdpSockOpts::JoinMulticast` - The configured socket option.
    pub fn join_multicast_v4(addr: Ipv4Addr) -> Self {
        Self::Udp(UdpSockOpts::JoinMulticast(addr))
    }

    /// Set the socket option to leave an IPv4 multicast group.
    ///
    /// # Arguments
    ///
    /// * `addr` - The IPv4 multicast address to leave.
    ///
    /// # Returns
    ///
    /// * `SocketOption::Udp(UdpSockOpts::LeaveMulticast` - The configured socket option.
    pub fn leave_multicast_v4(addr: Ipv4Addr) -> Self {
        Self::Udp(UdpSockOpts::LeaveMulticast(addr))
    }

    /// Set the socket option to enable or disable the UDP send callback.
    ///
    /// # Arguments
    ///
    /// * `status` - Whether to enable (`true`) or disable (`false`) the UDP send callback.
    ///
    /// # Returns
    ///
    /// * `SocketOption::Udp(UdpSockOpts::SetUdpSendCallback` - The configured socket option.
    pub fn set_udp_send_callback(status: bool) -> Self {
        Self::Udp(UdpSockOpts::SetUdpSendCallback(status))
    }

    /// Set a socket option to configure the UDP socket receive timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Timeout duration in milliseconds.
    ///
    /// # Returns
    ///
    /// * `SocketOption::Udp(UdpSockOpts::ReceiveTimeout` - The configured socket option.
    pub fn set_udp_receive_timeout(timeout: u32) -> Self {
        Self::Udp(UdpSockOpts::ReceiveTimeout(timeout))
    }

    /// Set a socket option to configure the TCP socket receive timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Timeout duration in milliseconds.
    ///
    /// # Returns
    ///
    /// * `SocketOption::Tcp(TcpSockOpts::ReceiveTimeout)` - The configured socket option.
    pub fn set_tcp_receive_timeout(timeout: u32) -> Self {
        Self::Tcp(TcpSockOpts::ReceiveTimeout(timeout))
    }

    /// Set the socket option to configure SNI (Server Name Indication) for TLS connections.
    ///
    /// # Arguments
    ///
    /// * `hostname` - The hostname to be used for SNI. Must not be greater then 63 bytes.
    ///
    /// # Returns
    ///
    /// * `SocketOptions` – The configured socket option on success.
    /// * `StackError` – If the hostname length is invalid.
    #[cfg(feature = "ssl")]
    pub fn set_sni(hostname: &str) -> Result<Self, StackError> {
        let host = HostName::from(hostname).map_err(|_| StackError::InvalidParameters)?;
        Ok(Self::Tcp(TcpSockOpts::Ssl(SslSockOpts::SetSni(host))))
    }

    /// Sets the socket option to configure SSL socket.
    ///
    /// # Arguments
    ///
    /// * `cfg` – The SSL socket configuration.
    /// * `enable` – `true` to enable the configuration, or `false` to disable it.
    ///
    /// # Returns
    ///
    /// * `SocketOptions` – The configured socket option on success.
    #[cfg(feature = "ssl")]
    pub fn config_ssl(cfg: SslSockConfig, enable: bool) -> Self {
        Self::Tcp(TcpSockOpts::Ssl(SslSockOpts::Config(cfg, enable)))
    }
}

impl<'a> AccessPoint<'a> {
    /// Creates a new access point configuration with the provided parameters.
    ///
    /// # Arguments
    ///
    /// * `ssid` - The SSID (network name) up to 32 characters.
    /// * `key` - Security credentials depends on the `auth` type.
    /// * `auth` - The authentication method (e.g., Open, WPA2).
    /// * `channel` - The Wi-Fi channel to operate on (typically between 1 and 14).
    /// * `ssid_hidden` - Whether the SSID should be hidden from network scans (true for hidden).
    /// * `ip` - The static IPv4 address to assign to the access point.
    ///
    /// # Notes
    ///
    /// For Open, the security type should be empty.
    /// For WPA, the security key must be at least 8 bytes (MIN) and no more than 63 bytes long.
    /// For WEP, the security key should be 10 bytes for 40-bit and 26 bytes for 104-bit.
    /// For S802, the security key should be no more then 40 bytes long.
    ///
    /// # Returns
    ///
    /// * `AccessPoint` - Configured access point structure on success.
    /// * `StackError` - If validation of any parameters fails.
    pub fn new(
        ssid: &'a Ssid,
        key: Credentials,
        channel: WifiChannel,
        ssid_hidden: bool,
        ip: Ipv4Addr,
    ) -> Result<Self, StackError> {
        let octets = ip.octets();
        let auth = <Credentials as Into<AuthType>>::into(key);

        if !((1..100).contains(&octets[3])) {
            return Err(StackError::InvalidParameters);
        }

        if auth == AuthType::S802_1X {
            return Err(StackError::InvalidParameters);
        }

        Ok(Self {
            ssid,
            key,
            channel,
            ssid_hidden,
            ip: Ipv4Addr::from(octets),
        })
    }

    /// Creates configuration for an open (no security) access point.
    ///
    /// # Arguments
    ///
    /// * `ssid` - The SSID (network name) string up to 32 bytes.
    ///
    /// # Returns
    ///
    /// * `AccessPoint` - The configured `AccessPoint` with open (no security) on success.
    pub fn open(ssid: &'a Ssid) -> Self {
        Self {
            ssid,
            key: Credentials::Open,
            channel: WifiChannel::Channel1,
            ssid_hidden: false,
            ip: Ipv4Addr::from_bits(DEFAULT_AP_IP),
        }
    }

    #[cfg(feature = "wep")]
    /// Creates configuration for a WEP-secured access point.
    ///
    /// # Arguments
    ///
    /// * `ssid` - The SSID (network name), up to 32 bytes.
    /// * `key` - The WEP security key, either 10 bytes (for 40-bit) or 26 bytes (for 104-bit).
    /// * `key_index` - Wep Key Index; typically between 0 and 4.
    ///
    /// # Returns
    ///
    /// * `AccessPoint` - The configured `AccessPoint` with WEP security on success.
    pub fn wep(ssid: &'a Ssid, key: &'a WepKey, key_index: WepKeyIndex) -> Self {
        Self {
            ssid,
            key: Credentials::Wep(*key, key_index),
            channel: WifiChannel::Channel1,
            ssid_hidden: false,
            ip: Ipv4Addr::from_bits(DEFAULT_AP_IP),
        }
    }

    /// Creates a configuration for a WPA or WPA2-secured access point.
    ///
    /// # Arguments
    ///
    /// * `ssid` - The SSID (network name), up to 32 bytes.
    /// * `key` - The WPA security key, up to 63 bytes (as per WPA/WPA2 specification).
    ///
    /// # Returns
    ///
    /// * `AccessPoint` - The configured `AccessPoint` with WPA-PSK security on success.
    pub fn wpa(ssid: &'a Ssid, key: &'a WpaKey) -> Self {
        Self {
            ssid,
            key: Credentials::WpaPSK(*key),
            channel: WifiChannel::Channel1,
            ssid_hidden: false,
            ip: Ipv4Addr::from_bits(DEFAULT_AP_IP),
        }
    }

    /// Sets the static IP address for the configured access point.
    ///
    /// # Arguments
    ///
    /// * `ip` - The new static IPv4 address to assign to the access point.
    ///
    /// # Warning
    ///
    /// Due to a WINC firmware limitation, the access point IP address can only be in the range `x.x.x.1` to `x.x.x.99`.
    ///
    /// # Returns
    ///
    /// * `()` - If the IP address is successfully set.
    /// * `StackError` - If the IP address is invalid.
    pub fn set_ip(&mut self, ip: Ipv4Addr) -> Result<(), StackError> {
        let octets = ip.octets();
        // WINC firmware limitation; IP address of client is always x.x.x.100
        if !((1..100).contains(&octets[3])) {
            return Err(StackError::InvalidParameters);
        }

        self.ip = Ipv4Addr::from(octets);

        Ok(())
    }

    /// Sets the Wi-Fi channel for the configured access point.
    ///
    /// # Arguments
    ///
    /// * `channel` - The Wi-Fi RF channel to use (typically 1–14).
    pub fn set_channel(&mut self, channel: WifiChannel) {
        self.channel = channel;
    }

    /// Sets whether the SSID is hidden.
    ///
    /// # Arguments
    ///
    /// * `hidden` – `true` to hide the SSID, `false` to make it visible.
    pub fn set_ssid_hidden(&mut self, hidden: bool) {
        self.ssid_hidden = hidden;
    }
}

impl MacAddress {
    /// Creates a new `MacAddress` from six individual octets.
    pub fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> MacAddress {
        Self {
            mac: [a, b, c, d, e, f],
        }
    }

    /// Creates a MAC address from a byte slice.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, StackError> {
        if bytes.len() != MAX_OCTETS_IN_MAC_ADDRESS {
            return Err(StackError::InvalidParameters);
        }

        let mut mac = [0u8; MAX_OCTETS_IN_MAC_ADDRESS];
        mac.copy_from_slice(bytes);

        Ok(Self { mac })
    }

    /// Creates an empty MAC address with all octets set to zero.
    pub fn empty() -> Self {
        Self {
            mac: [0u8; MAX_OCTETS_IN_MAC_ADDRESS],
        }
    }

    /// Returns the MAC address as a fixed-size array of octets.
    #[inline]
    pub fn octets(&self) -> [u8; MAX_OCTETS_IN_MAC_ADDRESS] {
        self.mac
    }

    /// Returns a mutable slice of the MAC address bytes.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        &mut self.mac
    }

    /// Returns an immutable slice of the MAC address bytes.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        &self.mac
    }
}

/// Implements the `core::fmt::Display` trait to display the MAC address in
/// hexadecimal format, with octets separated by colons.
/// For example: `AA:BB:CC:DD:EE:FF`.
#[cfg(feature = "log")]
impl core::fmt::Display for MacAddress {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
            self.mac[0], self.mac[1], self.mac[2], self.mac[3], self.mac[4], self.mac[5]
        )
    }
}

/// Implements the `defmt::Format` trait to display the MAC address in
/// hexadecimal format, with octets separated by colons.
/// For example: `AA:BB:CC:DD:EE:FF`.
#[cfg(feature = "defmt")]
impl defmt::Format for MacAddress {
    fn format(&self, f: defmt::Formatter) {
        defmt::write!(
            f,
            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
            self.mac[0],
            self.mac[1],
            self.mac[2],
            self.mac[3],
            self.mac[4],
            self.mac[5]
        )
    }
}

#[cfg(test)]
mod tests {
    use core::str::FromStr;

    use super::*;

    #[test]
    fn test_ap_set_channel() {
        let ssid = Ssid::from("test").unwrap();
        let mut ap = AccessPoint::open(&ssid);

        assert_eq!(ap.channel, WifiChannel::Channel1);

        ap.set_channel(WifiChannel::Channel2);

        assert_eq!(ap.channel, WifiChannel::Channel2);
    }

    #[test]
    fn test_ap_set_ip_fail() {
        let ssid = Ssid::from("test").unwrap();
        let psk = WpaKey::from("test_key").unwrap();
        let mut ap = AccessPoint::wpa(&ssid, &psk);
        let ip = Ipv4Addr::new(192, 168, 1, 100);

        assert_eq!(ap.ip, Ipv4Addr::from_bits(DEFAULT_AP_IP));

        let result = ap.set_ip(ip);

        assert_eq!(result, Err(StackError::InvalidParameters))
    }

    #[test]
    fn test_ap_set_ip_success() {
        let ssid = Ssid::from("test").unwrap();
        let psk = WpaKey::from("test_key").unwrap();
        let mut ap = AccessPoint::wpa(&ssid, &psk);
        let ip = Ipv4Addr::new(192, 168, 1, 1);

        assert_eq!(ap.ip, Ipv4Addr::from_bits(DEFAULT_AP_IP));

        let result = ap.set_ip(ip);

        assert!(result.is_ok());
    }

    #[test]
    fn test_ap_config_fail() {
        let ssid = Ssid::from("test").unwrap();
        let psk = WpaKey::from("test_key").unwrap();
        // Access Point Configuration.
        let ap = AccessPoint::new(
            &ssid,
            Credentials::WpaPSK(psk),
            WifiChannel::Channel1,
            false,
            Ipv4Addr::new(192, 168, 1, 100),
        );

        assert_eq!(ap, Err(StackError::InvalidParameters));
    }

    #[test]
    fn test_ap_config_success() {
        let ssid = Ssid::from("test").unwrap();
        let psk = WpaKey::from("test_key").unwrap();
        // Access Point Configuration.
        let ap = AccessPoint::new(
            &ssid,
            Credentials::WpaPSK(psk),
            WifiChannel::Channel1,
            false,
            Ipv4Addr::new(192, 168, 1, 1),
        );

        assert!(ap.is_ok());
    }

    #[test]
    fn test_ap_config_enterprise_fail() {
        let ssid = Ssid::from("test").unwrap();
        let username = S8Username::from("username").unwrap();
        let password = S8Password::from("password").unwrap();
        // Access Point Configuration.
        let ap = AccessPoint::new(
            &ssid,
            Credentials::S802_1X(username, password),
            WifiChannel::Channel1,
            false,
            Ipv4Addr::new(192, 168, 1, 1),
        );

        assert_eq!(ap, Err(StackError::InvalidParameters));
    }

    #[test]
    fn test_ssid_visibility() {
        let ssid = Ssid::from("test").unwrap();
        let mut ap = AccessPoint::open(&ssid);

        assert_eq!(ap.ssid_hidden, false);

        ap.set_ssid_hidden(true);

        assert_eq!(ap.ssid_hidden, true);
    }

    #[test]
    fn test_wpa_credentials_with_short_password() {
        let result = Credentials::from_wpa("pass");
        assert_eq!(result.err(), Some(StackError::InvalidParameters));
    }

    #[test]
    fn test_wpa_credentials_with_long_password() {
        let long_password = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        let result = Credentials::from_wpa(long_password);
        assert!(result.is_err());
    }

    #[test]
    fn test_wpa_credentials_with_valid_password() {
        let result = Credentials::from_wpa("ABCDEFGHIJKLM");
        assert!(result.is_ok());
    }

    #[test]
    fn test_s802_credentials_invalid_username() {
        let username = "abcdefghijklmnopqrst123";
        let password = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
        let result = Credentials::from_s802(username, password);
        assert_eq!(result.err(), Some(StackError::InvalidParameters));
    }

    #[test]
    fn test_s802_credentials_invalid_password() {
        let username = "abcdefghijklmnopqrst";
        let password = "abcdefghijklmnopqrstuvwxyz1234567890ABC123D";
        let result = Credentials::from_s802(username, password);
        assert_eq!(result.err(), Some(StackError::InvalidParameters));
    }

    #[test]
    fn test_s802_credentials_valid() {
        let username = "abcdefghijklmnopqrst";
        let password = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
        let result = Credentials::from_s802(username, password);
        assert!(result.is_ok());
    }

    #[test]
    fn test_s802_key_len() {
        let username = "abcdefghijklmnopqrst";
        let password = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
        let result = Credentials::from_s802(username, password);
        assert!(result.is_ok());

        assert_eq!(result.unwrap().key_len(), password.len());
    }

    #[test]
    fn test_s802_auth_type() {
        let username = "abcdefghijklmnopqrst";
        let password = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
        let result = Credentials::from_s802(username, password);
        assert!(result.is_ok());

        let s802_auth: u8 = result.unwrap().into();

        assert_eq!(s802_auth, <AuthType as Into<u8>>::into(AuthType::S802_1X));
    }

    #[cfg(feature = "wep")]
    #[test]
    fn test_wep_credentials_with_small_key() {
        let result = Credentials::from_wep("ABCDEFG", WepKeyIndex::Key1);
        assert!(result.is_err());
    }

    #[cfg(feature = "wep")]
    #[test]
    fn test_wep_credentials_with_large_key() {
        let long_password = "ABCDEFGlmnopqrstuvwxyz0123456789+/";
        let result = Credentials::from_wep(long_password, WepKeyIndex::Key2);
        assert!(result.is_err());
    }

    #[cfg(feature = "wep")]
    #[test]
    fn test_wep_credentials_with_104_bit_key() {
        let key = "0123456789ABCDEF0123456789";
        let result = Credentials::from_wep(key, WepKeyIndex::Key3);
        assert!(result.is_ok());
    }

    #[test]
    fn test_sock_opts_get_join_multicast_value_success() {
        let test_value: u32 = 0x101a8c0;
        let addr = Ipv4Addr::from_str("192.168.1.1").unwrap();
        let sock_opt = SocketOptions::join_multicast_v4(addr);

        if let SocketOptions::Udp(opt) = sock_opt {
            assert_eq!(test_value, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_join_multicast_value_fail() {
        let test_value: u32 = 0xc0a80101;
        let addr = Ipv4Addr::from_str("192.168.1.1").unwrap();
        let sock_opt = SocketOptions::join_multicast_v4(addr);

        if let SocketOptions::Udp(opt) = sock_opt {
            assert_ne!(test_value, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_leave_multicast_value_success() {
        let test_value: u32 = 0x101a8c0;
        let addr = Ipv4Addr::from_str("192.168.1.1").unwrap();
        let sock_opt = SocketOptions::join_multicast_v4(addr);

        if let SocketOptions::Udp(opt) = sock_opt {
            assert_eq!(test_value, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_leave_multicast_value_fail() {
        let test_value: u32 = 0xc0a80101;
        let addr = Ipv4Addr::from_str("192.168.1.1").unwrap();
        let sock_opt = SocketOptions::leave_multicast_v4(addr);

        if let SocketOptions::Udp(opt) = sock_opt {
            assert_ne!(test_value, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_udp_send_callback_false_value() {
        let sock_opts = SocketOptions::set_udp_send_callback(false);

        if let SocketOptions::Udp(opt) = sock_opts {
            assert_eq!(0u32, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_udp_send_callback_true_value() {
        let sock_opts = SocketOptions::set_udp_send_callback(true);

        if let SocketOptions::Udp(opt) = sock_opts {
            assert_eq!(1u32, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_udp_recv_timeout_value() {
        let test_value = 1500u32;
        let sock_opts = SocketOptions::set_udp_receive_timeout(test_value);

        if let SocketOptions::Udp(opt) = sock_opts {
            assert_eq!(test_value, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_get_tcp_recv_timeout_value() {
        let test_value = 2500u32;
        let sock_opts = SocketOptions::set_tcp_receive_timeout(test_value);

        if let SocketOptions::Tcp(opt) = sock_opts {
            assert_eq!(test_value, opt.get_value());
        } else {
            assert!(false);
        }
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_sock_opts_get_sni_array_success() {
        let test_value = "hostname".as_bytes();
        let sock_opts = SocketOptions::set_sni("hostname").unwrap();

        if let SocketOptions::Tcp(opt) = sock_opts {
            if let TcpSockOpts::Ssl(ssl) = opt {
                assert_eq!(ssl.get_sni_value().unwrap().as_bytes(), test_value);
            } else {
                assert!(false);
            }
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_sock_opts_udp_join_multicast_u8_value() {
        let addr = Ipv4Addr::from_str("192.168.1.1").unwrap();
        let sock_opts = UdpSockOpts::JoinMulticast(addr);
        assert_eq!(u8::from(sock_opts), 0x01u8);
    }

    #[test]
    fn test_sock_opts_udp_leave_multicast_u8_value() {
        let addr = Ipv4Addr::from_str("192.168.1.1").unwrap();
        let sock_opts = UdpSockOpts::LeaveMulticast(addr);
        assert_eq!(u8::from(sock_opts), 0x02u8);
    }

    #[test]
    fn test_sock_opts_udp_receive_timeout_u8_value() {
        let sock_opts = UdpSockOpts::ReceiveTimeout(1500);
        assert_eq!(u8::from(sock_opts), 0xffu8);
    }

    #[test]
    fn test_sock_opts_udp_send_callback_u8_value() {
        let sock_opts = UdpSockOpts::SetUdpSendCallback(false);
        assert_eq!(u8::from(sock_opts), 0x00u8);
    }

    #[test]
    fn test_sock_opts_tcp_receive_timeout_u8_value() {
        let sock_opts = TcpSockOpts::ReceiveTimeout(1500);
        assert_eq!(u8::from(sock_opts), 0xffu8);
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_sock_opts_tcp_ssl_u8_value() {
        let host = HostName::from("hostname").unwrap();
        let sock_opts = TcpSockOpts::Ssl(SslSockOpts::SetSni(host));
        assert_eq!(u8::from(sock_opts), 0xfeu8);
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_sock_opts_get_tcp_ssl_sni_value() {
        let host = HostName::from("hostname").unwrap();
        let sock_opts = TcpSockOpts::Ssl(SslSockOpts::SetSni(host));
        assert_eq!(sock_opts.get_value(), 0xfeu32);
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_sock_opts_ssl_sni_u8_value() {
        let host = HostName::from("hostname").unwrap();
        let sock_opts = SslSockOpts::SetSni(host);
        assert_eq!(u8::from(sock_opts), 0x02u8);
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_sock_opts_ssl_sni_invalid_parameter() {
        let test_string =
            "This is a test string that definitely contains more than sixty-three bytes of data.";
        let sock_opts = SocketOptions::set_sni(test_string);
        assert_eq!(sock_opts.err(), Some(StackError::InvalidParameters));
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_ssl_sock_opt_conv() {
        let sock_opt = SslSockOpts::Config(SslSockConfig::EnableSessionCache, true);

        let u8_value = u8::from(sock_opt);

        assert_eq!(u8_value, u8::from(SslSockConfig::EnableSessionCache));
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_ssl_sock_opt_ref_conv() {
        let sock_opt = SslSockOpts::Config(SslSockConfig::EnableSniValidation, true);
        let sock_opt_ref = &sock_opt;

        let u8_value = u8::from(sock_opt_ref);

        assert_eq!(u8_value, u8::from(SslSockConfig::EnableSniValidation));
    }

    #[cfg(feature = "ssl")]
    #[test]
    fn test_ssl_sock_opt_invalid_opt() {
        let sock_opt = SslSockOpts::Config(SslSockConfig::EnableSniValidation, true);

        let res = sock_opt.get_sni_value();

        assert_eq!(res, Err(StackError::InvalidParameters));
    }

    #[test]
    fn test_create_new_mac_address() {
        let mac = MacAddress::new(0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6);
        assert_eq!(mac.octets(), [0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6]);

        let mac = MacAddress::empty();
        assert_eq!(mac.octets(), [0u8; MAX_OCTETS_IN_MAC_ADDRESS]);
    }

    #[test]
    fn test_create_mac_address_from_array_success() {
        let bytes = [0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6];
        let mac = MacAddress::from_bytes(&bytes);

        assert!(mac.is_ok());
        assert_eq!(mac.unwrap().as_slice(), bytes);
    }

    #[test]
    fn test_create_mac_address_from_array_failure() {
        let bytes = [0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0xAA];
        let mac = MacAddress::from_bytes(&bytes);

        assert_eq!(mac.err(), Some(StackError::InvalidParameters));
    }

    #[test]
    fn test_create_mac_address_mut_array() {
        let bytes = [0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6];
        let mut mac = MacAddress::from_bytes(&bytes).unwrap();
        let mod_mac = mac.as_mut_slice();

        for (index, byte) in mod_mac.iter_mut().enumerate() {
            *byte = (index * 2) as u8;
        }

        assert_ne!(mod_mac, &bytes);
    }

    #[cfg(all(feature = "log", feature = "std"))]
    #[test]
    fn test_format_mac_address() {
        use std::format;

        let bytes = [0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6];
        let mac = MacAddress::from_bytes(&bytes);

        assert_eq!(format!("{}", mac.unwrap()), "A1:B2:C3:D4:E5:F6");
    }
}