forked from ivmarkov/edge-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
675 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "edge-nal-embassy" | ||
version = "0.2.0" | ||
edition = "2021" | ||
rust-version = "1.77" | ||
description = "Am implementation of edge-nal based on `embassy-net`" | ||
repository = "https://github.com/ivmarkov/edge-net" | ||
readme = "README.md" | ||
license = "MIT OR Apache-2.0" | ||
categories = [ | ||
"embedded", | ||
"no-std", | ||
"asynchronous", | ||
"networking" | ||
] | ||
|
||
[dependencies] | ||
embedded-io-async = { workspace = true } | ||
edge-nal = { workspace = true } | ||
heapless = { workspace = true } | ||
embassy-net = { version = "0.4", features = ["tcp", "udp", "dns", "proto-ipv6", "medium-ethernet", "proto-ipv4"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# edge-nal-embassy | ||
|
||
[![CI](https://github.com/ivmarkov/edge-net/actions/workflows/ci.yml/badge.svg)](https://github.com/ivmarkov/edge-net/actions/workflows/ci.yml) | ||
![crates.io](https://img.shields.io/crates/v/edge-net.svg) | ||
[![Documentation](https://docs.rs/edge-net/badge.svg)](https://docs.rs/edge-net) | ||
|
||
A bare-metal implementation of `edge-nal` based on [embassy-net](). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#![no_std] | ||
#![allow(async_fn_in_trait)] | ||
#![warn(clippy::large_futures)] | ||
|
||
use core::cell::{Cell, UnsafeCell}; | ||
use core::mem::MaybeUninit; | ||
use core::net::{IpAddr, Ipv6Addr, SocketAddr}; | ||
use core::ptr::NonNull; | ||
|
||
use embassy_net::{IpAddress, IpEndpoint, IpListenEndpoint}; | ||
|
||
pub use tcp::*; | ||
pub use udp::*; | ||
|
||
mod tcp; | ||
mod udp; | ||
|
||
pub(crate) struct Pool<T, const N: usize> { | ||
used: [Cell<bool>; N], | ||
data: [UnsafeCell<MaybeUninit<T>>; N], | ||
} | ||
|
||
impl<T, const N: usize> Pool<T, N> { | ||
const VALUE: Cell<bool> = Cell::new(false); | ||
const UNINIT: UnsafeCell<MaybeUninit<T>> = UnsafeCell::new(MaybeUninit::uninit()); | ||
|
||
const fn new() -> Self { | ||
Self { | ||
used: [Self::VALUE; N], | ||
data: [Self::UNINIT; N], | ||
} | ||
} | ||
} | ||
|
||
impl<T, const N: usize> Pool<T, N> { | ||
fn alloc(&self) -> Option<NonNull<T>> { | ||
for n in 0..N { | ||
// this can't race because Pool is not Sync. | ||
if !self.used[n].get() { | ||
self.used[n].set(true); | ||
let p = self.data[n].get() as *mut T; | ||
return Some(unsafe { NonNull::new_unchecked(p) }); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// safety: p must be a pointer obtained from self.alloc that hasn't been freed yet. | ||
unsafe fn free(&self, p: NonNull<T>) { | ||
let origin = self.data.as_ptr() as *mut T; | ||
let n = p.as_ptr().offset_from(origin); | ||
assert!(n >= 0); | ||
assert!((n as usize) < N); | ||
self.used[n as usize].set(false); | ||
} | ||
} | ||
|
||
pub(crate) fn to_net_socket(socket: IpEndpoint) -> SocketAddr { | ||
SocketAddr::new(to_net_addr(socket.addr), socket.port) | ||
} | ||
|
||
pub(crate) fn to_net_socket2(socket: IpListenEndpoint) -> SocketAddr { | ||
SocketAddr::new( | ||
socket | ||
.addr | ||
.map(to_net_addr) | ||
.unwrap_or(IpAddr::V6(Ipv6Addr::UNSPECIFIED)), | ||
socket.port, | ||
) | ||
} | ||
|
||
pub(crate) fn to_emb_socket(socket: SocketAddr) -> IpEndpoint { | ||
IpEndpoint { | ||
addr: to_emb_addr(socket.ip()), | ||
port: socket.port(), | ||
} | ||
} | ||
|
||
pub(crate) fn to_net_addr(addr: IpAddress) -> IpAddr { | ||
match addr { | ||
//#[cfg(feature = "proto-ipv4")] | ||
IpAddress::Ipv4(addr) => addr.0.into(), | ||
// #[cfg(not(feature = "proto-ipv4"))] | ||
// IpAddr::V4(_) => panic!("ipv4 support not enabled"), | ||
//#[cfg(feature = "proto-ipv6")] | ||
IpAddress::Ipv6(addr) => addr.0.into(), | ||
// #[cfg(not(feature = "proto-ipv6"))] | ||
// IpAddr::V6(_) => panic!("ipv6 support not enabled"), | ||
} | ||
} | ||
|
||
pub(crate) fn to_emb_addr(addr: IpAddr) -> IpAddress { | ||
match addr { | ||
//#[cfg(feature = "proto-ipv4")] | ||
IpAddr::V4(addr) => IpAddress::Ipv4(embassy_net::Ipv4Address::from_bytes(&addr.octets())), | ||
// #[cfg(not(feature = "proto-ipv4"))] | ||
// IpAddr::V4(_) => panic!("ipv4 support not enabled"), | ||
//#[cfg(feature = "proto-ipv6")] | ||
IpAddr::V6(addr) => IpAddress::Ipv6(embassy_net::Ipv6Address::from_bytes(&addr.octets())), | ||
// #[cfg(not(feature = "proto-ipv6"))] | ||
// IpAddr::V6(_) => panic!("ipv6 support not enabled"), | ||
} | ||
} |
Oops, something went wrong.