Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic IPv6 Support #181

Merged
merged 1 commit into from May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -41,7 +41,7 @@ smoltcp = { version = "0.9.1", default-features=false, features = ["proto-igmp",
critical-section = "1.1.1"
atomic-polyfill = "1.0.1"
log = "0.4.17"
embedded-svc = { version = "0.24.0", default-features = false, features = [] }
embedded-svc = { version = "0.25.0", default-features = false, features = [] }
enumset = { version = "1", default-features = false }
linked_list_allocator = { version = "0.10.3", default-features = false, features = ["const_mut_refs"] }
embedded-io = "0.4.0"
Expand Down
1 change: 1 addition & 0 deletions esp-wifi/Cargo.toml
Expand Up @@ -71,3 +71,4 @@ mtu-1514 = []
mtu-1500 = []
mtu-1492 = []
mtu-746 = []
ipv6 = ["smoltcp?/proto-ipv6", "embassy-net?/proto-ipv6"]
5 changes: 3 additions & 2 deletions esp-wifi/src/wifi/mod.rs
Expand Up @@ -782,13 +782,15 @@ impl<'d> WifiController<'d> {
Ok(())
}

#[allow(unused)]
fn is_sta_enabled(&self) -> Result<bool, WifiError> {
let mut mode: esp_wifi_sys::include::wifi_mode_t = 0;
esp_wifi_result!(unsafe { esp_wifi_sys::include::esp_wifi_get_mode(&mut mode) })?;

Ok(mode == wifi_mode_t_WIFI_MODE_STA)
}

#[allow(unused)]
fn is_ap_enabled(&self) -> Result<bool, WifiError> {
let mut mode: esp_wifi_sys::include::wifi_mode_t = 0;
esp_wifi_result!(unsafe { esp_wifi_sys::include::esp_wifi_get_mode(&mut mode) })?;
Expand Down Expand Up @@ -1215,9 +1217,8 @@ impl embedded_svc::wifi::Wifi for WifiController<'_> {
match crate::wifi::get_wifi_state() {
crate::wifi::WifiState::Invalid => Ok(false),
// We assume that wifi has been started in every other states
_ => Ok(true)
_ => Ok(true),
}

}

fn is_connected(&self) -> Result<bool, Self::Error> {
Expand Down
66 changes: 56 additions & 10 deletions esp-wifi/src/wifi_interface.rs
Expand Up @@ -42,7 +42,7 @@ impl<'a> WifiStack<'a> {
}
}

Self {
let this = Self {
device: RefCell::new(device),
network_interface: RefCell::new(network_interface),
network_config: RefCell::new(ipv4::Configuration::Client(
Expand All @@ -56,7 +56,11 @@ impl<'a> WifiStack<'a> {
sockets: RefCell::new(sockets),
current_millis_fn,
local_port: RefCell::new(41000),
}
};

this.reset();

this
}

pub fn update_iface_configuration(
Expand Down Expand Up @@ -131,9 +135,53 @@ impl<'a> WifiStack<'a> {
interface.update_ip_addrs(|addrs| {
addrs.clear();
});

#[cfg(feature = "ipv6")]
{
interface
.routes_mut()
.add_default_ipv6_route(smoltcp::wire::Ipv6Address::new(
0xfe80, 0, 0, 0, 0, 0, 0, 0,
))
.unwrap();

let mut mac = [0u8; 6];
match interface.hardware_addr() {
smoltcp::wire::HardwareAddress::Ethernet(hw_address) => {
mac.copy_from_slice(hw_address.as_bytes());
}
}

let a4 = ((mac[0] ^ 2) as u16) << 8 | mac[1] as u16;
let a5 = (mac[2] as u16) << 8 | 0xff;
let a6 = 0xfe << 8 | mac[3] as u16;
let a7 = (mac[4] as u16) << 8 | mac[5] as u16;

log::info!(
"IPv6 link-local address fe80::{:x}:{:x}:{:x}:{:x}",
a4,
a5,
a6,
a7
);

interface.update_ip_addrs(|addrs| {
addrs
.push(IpCidr::new(
smoltcp::wire::IpAddress::v6(0xfe80, 0, 0, 0, a4, a5, a6, a7),
64,
))
.unwrap();
});
}
});
}

/// Retrieve all current IP addresses
pub fn get_ip_addresses(&self, f: impl FnOnce(&[smoltcp::wire::IpCidr])) {
self.with_mut(|interface, _, _| f(interface.ip_addrs()))
}

/// Convenience function to poll the DHCP socket.
pub fn poll_dhcp(
&self,
Expand Down Expand Up @@ -333,7 +381,7 @@ pub struct Socket<'s, 'n: 's> {
}

impl<'s, 'n: 's> Socket<'s, 'n> {
pub fn open<'i>(&'i mut self, addr: Ipv4Address, port: u16) -> Result<(), IoError>
pub fn open<'i>(&'i mut self, addr: IpAddress, port: u16) -> Result<(), IoError>
where
's: 'i,
{
Expand Down Expand Up @@ -636,7 +684,7 @@ impl<'s, 'n: 's> UdpSocket<'s, 'n> {
self.work();
}

pub fn send(&mut self, addr: Ipv4Address, port: u16, data: &[u8]) -> Result<(), IoError> {
pub fn send(&mut self, addr: IpAddress, port: u16, data: &[u8]) -> Result<(), IoError> {
loop {
self.work();

Expand Down Expand Up @@ -670,7 +718,7 @@ impl<'s, 'n: 's> UdpSocket<'s, 'n> {
Ok(())
}

pub fn receive(&mut self, data: &mut [u8]) -> Result<(usize, [u8; 4], u16), IoError> {
pub fn receive(&mut self, data: &mut [u8]) -> Result<(usize, IpAddress, u16), IoError> {
self.work();

let res = self.network.with_mut(|_interface, _device, sockets| {
Expand All @@ -681,16 +729,14 @@ impl<'s, 'n: 's> UdpSocket<'s, 'n> {

match res {
Ok((len, endpoint)) => {
let addr = match endpoint.addr {
IpAddress::Ipv4(ipv4) => ipv4,
};
Ok((len, addr.0, endpoint.port))
let addr = endpoint.addr;
Ok((len, addr, endpoint.port))
}
Err(e) => Err(IoError::UdpRecvError(e)),
}
}

pub fn join_multicast_group(&mut self, addr: Ipv4Address) -> Result<bool, IoError> {
pub fn join_multicast_group(&mut self, addr: IpAddress) -> Result<bool, IoError> {
self.work();

let res = self.network.with_mut(|interface, device, _| {
Expand Down
1 change: 1 addition & 0 deletions examples-esp32/Cargo.toml
Expand Up @@ -40,3 +40,4 @@ ble = ["esp-wifi/ble"]
phy-enable-usb = ["esp-wifi/phy-enable-usb"]
ps-min-modem = ["esp-wifi/ps-min-modem"]
esp-now = ["esp-wifi/esp-now"]
ipv6 = ["esp-wifi/ipv6"]
4 changes: 2 additions & 2 deletions examples-esp32/examples/coex.rs
Expand Up @@ -28,7 +28,7 @@ use hal::{
Rng,
};
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::{iface::SocketStorage, wire::Ipv4Address};
use smoltcp::{iface::SocketStorage, wire::IpAddress, wire::Ipv4Address};

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -140,7 +140,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
3 changes: 2 additions & 1 deletion examples-esp32/examples/dhcp.rs
Expand Up @@ -18,6 +18,7 @@ use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::IpAddress;
use smoltcp::wire::Ipv4Address;

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
1 change: 1 addition & 0 deletions examples-esp32c2/Cargo.toml
Expand Up @@ -41,3 +41,4 @@ ble = ["esp-wifi/ble"]
phy-enable-usb = ["esp-wifi/phy-enable-usb"]
ps-min-modem = ["esp-wifi/ps-min-modem"]
esp-now = ["esp-wifi/esp-now"]
ipv6 = ["esp-wifi/ipv6"]
3 changes: 2 additions & 1 deletion examples-esp32c2/examples/dhcp.rs
Expand Up @@ -18,6 +18,7 @@ use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::IpAddress;
use smoltcp::wire::Ipv4Address;

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
1 change: 1 addition & 0 deletions examples-esp32c3/Cargo.toml
Expand Up @@ -41,3 +41,4 @@ ble = ["esp-wifi/ble"]
phy-enable-usb = ["esp-wifi/phy-enable-usb"]
ps-min-modem = ["esp-wifi/ps-min-modem"]
esp-now = ["esp-wifi/esp-now"]
ipv6 = ["esp-wifi/ipv6"]
2 changes: 1 addition & 1 deletion examples-esp32c3/examples/async_ble.rs
Expand Up @@ -12,7 +12,7 @@ use bleps::{
async_attribute_server::AttributeServer,
asynch::Ble,
attribute_server::NotificationData,
gatt
gatt,
};
use embassy_executor::Executor;
use embassy_executor::_export::StaticCell;
Expand Down
8 changes: 5 additions & 3 deletions examples-esp32c3/examples/ble.rs
Expand Up @@ -115,9 +115,11 @@ fn main() -> ! {
debounce_cnt -= 1;
if debounce_cnt == 0 {
let mut cccd = [0u8; 1];
if let Some(1) =
srv.get_characteristic_value(my_characteristic_notify_enable_handle, 0, &mut cccd)
{
if let Some(1) = srv.get_characteristic_value(
my_characteristic_notify_enable_handle,
0,
&mut cccd,
) {
// if notifications enabled
if cccd[0] == 1 {
notification = Some(NotificationData::new(
Expand Down
4 changes: 2 additions & 2 deletions examples-esp32c3/examples/coex.rs
Expand Up @@ -28,7 +28,7 @@ use hal::{
Rng,
};
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::{iface::SocketStorage, wire::Ipv4Address};
use smoltcp::{iface::SocketStorage, wire::IpAddress, wire::Ipv4Address};

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -140,7 +140,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
3 changes: 2 additions & 1 deletion examples-esp32c3/examples/dhcp.rs
Expand Up @@ -18,6 +18,7 @@ use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::IpAddress;
use smoltcp::wire::Ipv4Address;

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
1 change: 1 addition & 0 deletions examples-esp32c6/Cargo.toml
Expand Up @@ -40,3 +40,4 @@ ble = ["esp-wifi/ble"]
phy-enable-usb = ["esp-wifi/phy-enable-usb"]
ps-min-modem = ["esp-wifi/ps-min-modem"]
esp-now = ["esp-wifi/esp-now"]
ipv6 = ["esp-wifi/ipv6"]
3 changes: 2 additions & 1 deletion examples-esp32c6/examples/dhcp.rs
Expand Up @@ -18,6 +18,7 @@ use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::IpAddress;
use smoltcp::wire::Ipv4Address;

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
1 change: 1 addition & 0 deletions examples-esp32s2/Cargo.toml
Expand Up @@ -40,3 +40,4 @@ ble = ["esp-wifi/ble"]
phy-enable-usb = ["esp-wifi/phy-enable-usb"]
ps-min-modem = ["esp-wifi/ps-min-modem"]
esp-now = ["esp-wifi/esp-now"]
ipv6 = ["esp-wifi/ipv6"]
3 changes: 2 additions & 1 deletion examples-esp32s2/examples/dhcp.rs
Expand Up @@ -18,6 +18,7 @@ use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::IpAddress;
use smoltcp::wire::Ipv4Address;

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
1 change: 1 addition & 0 deletions examples-esp32s3/Cargo.toml
Expand Up @@ -41,3 +41,4 @@ ble = ["esp-wifi/ble"]
phy-enable-usb = ["esp-wifi/phy-enable-usb"]
ps-min-modem = ["esp-wifi/ps-min-modem"]
esp-now = ["esp-wifi/esp-now"]
ipv6 = ["esp-wifi/ipv6"]
4 changes: 2 additions & 2 deletions examples-esp32s3/examples/coex.rs
Expand Up @@ -28,7 +28,7 @@ use hal::{
Rng,
};
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::{iface::SocketStorage, wire::Ipv4Address};
use smoltcp::{iface::SocketStorage, wire::IpAddress, wire::Ipv4Address};

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down Expand Up @@ -140,7 +140,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down
3 changes: 2 additions & 1 deletion examples-esp32s3/examples/dhcp.rs
Expand Up @@ -18,6 +18,7 @@ use hal::clock::{ClockControl, CpuClock};
use hal::Rng;
use hal::{peripherals::Peripherals, prelude::*, Rtc};
use smoltcp::iface::SocketStorage;
use smoltcp::wire::IpAddress;
use smoltcp::wire::Ipv4Address;

const SSID: &str = env!("SSID");
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> ! {
socket.work();

socket
.open(Ipv4Address::new(142, 250, 185, 115), 80)
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
.unwrap();

socket
Expand Down