esp_idf_svc/private/
net.rs

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
use core::convert::{TryFrom, TryInto};

use crate::sys::*;
use embedded_svc::ipv4::{self, Mask};

use crate::private::common::*;

impl From<ipv4::Ipv4Addr> for Newtype<esp_ip4_addr_t> {
    fn from(ip: ipv4::Ipv4Addr) -> Self {
        Newtype(esp_ip4_addr_t {
            addr: u32::to_be(u32::from_be_bytes(ip.octets())),
        })
    }
}

impl From<Newtype<esp_ip4_addr_t>> for ipv4::Ipv4Addr {
    fn from(ip: Newtype<esp_ip4_addr_t>) -> Self {
        ipv4::Ipv4Addr::from(u32::from_be(ip.0.addr))
    }
}

impl From<ipv4::Ipv4Addr> for Newtype<ip4_addr_t> {
    fn from(ip: ipv4::Ipv4Addr) -> Self {
        let result: Newtype<esp_ip4_addr_t> = ip.into();

        Newtype(ip4_addr_t {
            addr: result.0.addr,
        })
    }
}

impl From<Newtype<ip4_addr_t>> for ipv4::Ipv4Addr {
    fn from(ip: Newtype<ip4_addr_t>) -> Self {
        Newtype(esp_ip4_addr_t { addr: ip.0.addr }).into()
    }
}

impl From<Mask> for Newtype<esp_ip4_addr_t> {
    fn from(mask: Mask) -> Self {
        let ip: ipv4::Ipv4Addr = mask.into();

        ip.into()
    }
}

impl TryFrom<Newtype<esp_ip4_addr_t>> for Mask {
    type Error = EspError;

    fn try_from(esp_ip: Newtype<esp_ip4_addr_t>) -> Result<Self, Self::Error> {
        let ip: ipv4::Ipv4Addr = esp_ip.into();

        ip.try_into()
            .map_err(|_| EspError::from_infallible::<ESP_ERR_INVALID_ARG>())
    }
}

impl From<Mask> for Newtype<ip4_addr_t> {
    fn from(mask: Mask) -> Self {
        let ip: ipv4::Ipv4Addr = mask.into();

        ip.into()
    }
}

impl TryFrom<Newtype<ip4_addr_t>> for Mask {
    type Error = EspError;

    fn try_from(esp_ip: Newtype<ip4_addr_t>) -> Result<Self, Self::Error> {
        let ip: ipv4::Ipv4Addr = esp_ip.into();

        ip.try_into()
            .map_err(|_| EspError::from_infallible::<ESP_ERR_INVALID_ARG>())
    }
}

impl From<ipv4::IpInfo> for Newtype<esp_netif_ip_info_t> {
    fn from(ip_info: ipv4::IpInfo) -> Self {
        Newtype(esp_netif_ip_info_t {
            ip: Newtype::<esp_ip4_addr_t>::from(ip_info.ip).0,
            netmask: Newtype::<esp_ip4_addr_t>::from(ip_info.subnet.mask).0,
            gw: Newtype::<esp_ip4_addr_t>::from(ip_info.subnet.gateway).0,
        })
    }
}

impl From<Newtype<esp_netif_ip_info_t>> for ipv4::IpInfo {
    fn from(ip_info: Newtype<esp_netif_ip_info_t>) -> Self {
        ipv4::IpInfo {
            ip: ipv4::Ipv4Addr::from(Newtype(ip_info.0.ip)),
            subnet: ipv4::Subnet {
                gateway: ipv4::Ipv4Addr::from(Newtype(ip_info.0.gw)),
                mask: Newtype(ip_info.0.netmask).try_into().unwrap(),
            },
            dns: None,
            secondary_dns: None,
        }
    }
}

impl From<Newtype<esp_ip6_addr_t>> for core::net::Ipv6Addr {
    fn from(value: Newtype<esp_ip6_addr_t>) -> Self {
        let mut out = [0; 16];
        value
            .0
            .addr
            .into_iter()
            .map(u32::from_be)
            .flat_map(u32::to_be_bytes)
            .zip(out.iter_mut())
            .for_each(|(i, o)| *o = i);

        core::net::Ipv6Addr::from(out)
    }
}