1use core::{
57 any::TypeId,
58 ptr::{self, NonNull},
59};
60
61use crate::{FrameKind, HeaderSeq, Key, ProtocolError};
62use cordyceps::{Linked, list::Links};
63
64pub mod raw;
65
66macro_rules! wrapper {
67 ($sto: ty, $($arr: ident)?) => {
68 #[repr(transparent)]
69 pub struct Socket<T, R, M, $(const $arr: usize)?>
70 where
71 T: serde::Serialize + Clone + serde::de::DeserializeOwned + 'static,
72 R: mutex::ScopedRawMutex + 'static,
73 M: $crate::interface_manager::InterfaceManager + 'static,
74 {
75 socket: $crate::socket::raw::Socket<$sto, T, R, M>,
76 }
77
78 pub struct SocketHdl<'a, T, R, M, $(const $arr: usize)?>
79 where
80 T: serde::Serialize + Clone + serde::de::DeserializeOwned + 'static,
81 R: mutex::ScopedRawMutex + 'static,
82 M: $crate::interface_manager::InterfaceManager + 'static,
83 {
84 hdl: $crate::socket::raw::SocketHdl<'a, $sto, T, R, M>,
85 }
86
87 pub struct Recv<'a, 'b, T, R, M, $(const $arr: usize)?>
88 where
89 T: serde::Serialize + Clone + serde::de::DeserializeOwned + 'static,
90 R: mutex::ScopedRawMutex + 'static,
91 M: $crate::interface_manager::InterfaceManager + 'static,
92 {
93 recv: $crate::socket::raw::Recv<'a, 'b, $sto, T, R, M>,
94 }
95
96 impl<T, R, M, $(const $arr: usize)?> Socket<T, R, M, $($arr)?>
97 where
98 T: serde::Serialize + Clone + serde::de::DeserializeOwned + 'static,
99 R: mutex::ScopedRawMutex + 'static,
100 M: $crate::interface_manager::InterfaceManager + 'static,
101 {
102 pub fn attach<'a>(self: core::pin::Pin<&'a mut Self>) -> SocketHdl<'a, T, R, M, $($arr)?> {
103 let socket: core::pin::Pin<&'a mut $crate::socket::raw::Socket<$sto, T, R, M>> = unsafe { self.map_unchecked_mut(|me| &mut me.socket) };
104 SocketHdl {
105 hdl: socket.attach(),
106 }
107 }
108
109 pub fn attach_broadcast<'a>(
110 self: core::pin::Pin<&'a mut Self>,
111 ) -> SocketHdl<'a, T, R, M, $($arr)?> {
112 let socket: core::pin::Pin<&'a mut $crate::socket::raw::Socket<$sto, T, R, M>> = unsafe { self.map_unchecked_mut(|me| &mut me.socket) };
113 SocketHdl {
114 hdl: socket.attach_broadcast(),
115 }
116 }
117
118 pub fn stack(&self) -> &'static crate::net_stack::NetStack<R, M> {
119 self.socket.stack()
120 }
121 }
122
123 impl<'a, T, R, M, $(const $arr: usize)?> SocketHdl<'a, T, R, M, $($arr)?>
124 where
125 T: serde::Serialize + Clone + serde::de::DeserializeOwned + 'static,
126 R: mutex::ScopedRawMutex + 'static,
127 M: $crate::interface_manager::InterfaceManager + 'static,
128 {
129 pub fn port(&self) -> u8 {
130 self.hdl.port()
131 }
132
133 pub fn stack(&self) -> &'static crate::net_stack::NetStack<R, M> {
134 self.hdl.stack()
135 }
136
137 pub fn recv<'b>(&'b mut self) -> Recv<'b, 'a, T, R, M, $($arr)?> {
141 Recv {
142 recv: self.hdl.recv(),
143 }
144 }
145 }
146
147 impl<T, R, M, $(const $arr: usize)?> Future for Recv<'_, '_, T, R, M, $($arr)?>
148 where
149 T: serde::Serialize + Clone + serde::de::DeserializeOwned + 'static,
150 R: mutex::ScopedRawMutex + 'static,
151 M: $crate::interface_manager::InterfaceManager + 'static,
152 {
153 type Output = $crate::socket::Response<T>;
154
155 fn poll(
156 self: core::pin::Pin<&mut Self>,
157 cx: &mut core::task::Context<'_>,
158 ) -> core::task::Poll<Self::Output> {
159 let recv: core::pin::Pin<&mut $crate::socket::raw::Recv<'_, '_, $sto, T, R, M>> = unsafe { self.map_unchecked_mut(|me| &mut me.recv) };
160 recv.poll(cx)
161 }
162 }
163 };
164}
165
166pub mod single {
167 use mutex::ScopedRawMutex;
168 use serde::{Serialize, de::DeserializeOwned};
169
170 use crate::{
171 Key,
172 interface_manager::InterfaceManager,
173 net_stack::NetStack,
174 socket::{Attributes, raw},
175 };
176
177 impl<T: 'static> raw::Storage<T> for Option<T> {
178 #[inline]
179 fn is_full(&self) -> bool {
180 self.is_some()
181 }
182
183 #[inline]
184 fn is_empty(&self) -> bool {
185 self.is_none()
186 }
187
188 #[inline]
189 fn push(&mut self, t: T) -> Result<(), raw::StorageFull> {
190 if self.is_some() {
191 return Err(raw::StorageFull);
192 }
193 *self = Some(t);
194 Ok(())
195 }
196
197 #[inline]
198 fn try_pop(&mut self) -> Option<T> {
199 self.take()
200 }
201 }
202
203 wrapper!(Option<super::Response<T>>,);
204
205 impl<T, R, M> Socket<T, R, M>
206 where
207 T: Serialize + Clone + DeserializeOwned + 'static,
208 R: ScopedRawMutex + 'static,
209 M: InterfaceManager + 'static,
210 {
211 #[inline]
212 pub const fn new(net: &'static NetStack<R, M>, key: Key, attrs: Attributes) -> Self {
213 Self {
214 socket: raw::Socket::new(net, key, attrs, None),
215 }
216 }
217 }
218}
219
220pub mod std_bounded {
221 use mutex::ScopedRawMutex;
222 use serde::{Serialize, de::DeserializeOwned};
223 use std::collections::VecDeque;
224
225 use crate::{Key, NetStack, interface_manager::InterfaceManager};
226
227 use super::{Attributes, raw};
228
229 pub struct Bounded<T> {
230 storage: std::collections::VecDeque<T>,
231 max_len: usize,
232 }
233
234 impl<T> Bounded<T> {
235 pub fn with_bound(bound: usize) -> Self {
236 Self {
237 storage: VecDeque::new(),
238 max_len: bound,
239 }
240 }
241 }
242
243 impl<T: 'static> raw::Storage<T> for Bounded<T> {
244 #[inline]
245 fn is_full(&self) -> bool {
246 self.storage.len() >= self.max_len
247 }
248
249 #[inline]
250 fn is_empty(&self) -> bool {
251 self.storage.is_empty()
252 }
253
254 #[inline]
255 fn push(&mut self, t: T) -> Result<(), raw::StorageFull> {
256 if self.is_full() {
257 return Err(raw::StorageFull);
258 }
259 self.storage.push_back(t);
260 Ok(())
261 }
262
263 #[inline]
264 fn try_pop(&mut self) -> Option<T> {
265 self.storage.pop_front()
266 }
267 }
268
269 wrapper!(Bounded<super::Response<T>>,);
270
271 impl<T, R, M> Socket<T, R, M>
272 where
273 T: Serialize + Clone + DeserializeOwned + 'static,
274 R: ScopedRawMutex + 'static,
275 M: InterfaceManager + 'static,
276 {
277 #[inline]
278 pub fn new(
279 net: &'static NetStack<R, M>,
280 key: Key,
281 attrs: Attributes,
282 bound: usize,
283 ) -> Self {
284 Self {
285 socket: raw::Socket::new(net, key, attrs, Bounded::with_bound(bound)),
286 }
287 }
288 }
289}
290
291pub mod stack_vec {
292 use mutex::ScopedRawMutex;
293 use serde::{Serialize, de::DeserializeOwned};
294
295 use crate::{Key, NetStack, interface_manager::InterfaceManager};
296
297 use super::{Attributes, raw};
298
299 pub struct Bounded<T: 'static, const N: usize> {
300 storage: heapless::Vec<T, N>,
301 }
302
303 impl<T: 'static, const N: usize> Bounded<T, N> {
304 pub const fn new() -> Self {
305 Self {
306 storage: heapless::Vec::new(),
307 }
308 }
309 }
310
311 impl<T: 'static, const N: usize> Default for Bounded<T, N> {
312 fn default() -> Self {
313 Self::new()
314 }
315 }
316
317 impl<T: 'static, const N: usize> raw::Storage<T> for Bounded<T, N> {
318 #[inline]
319 fn is_full(&self) -> bool {
320 self.storage.is_full()
321 }
322
323 #[inline]
324 fn is_empty(&self) -> bool {
325 self.storage.is_empty()
326 }
327
328 #[inline]
329 fn push(&mut self, t: T) -> Result<(), raw::StorageFull> {
330 self.storage.push(t).map_err(|_| raw::StorageFull)
331 }
332
333 #[inline]
334 fn try_pop(&mut self) -> Option<T> {
335 self.storage.pop()
336 }
337 }
338
339 wrapper!(Bounded<super::Response<T>, N>, N);
340
341 impl<T, R, M, const N: usize> Socket<T, R, M, N>
342 where
343 T: Serialize + Clone + DeserializeOwned + 'static,
344 R: ScopedRawMutex + 'static,
345 M: InterfaceManager + 'static,
346 {
347 #[inline]
348 pub const fn new(net: &'static NetStack<R, M>, key: Key, attrs: Attributes) -> Self {
349 Self {
350 socket: raw::Socket::new(net, key, attrs, Bounded::new()),
351 }
352 }
353 }
354}
355
356#[derive(Debug)]
357pub struct Attributes {
358 pub kind: FrameKind,
359 pub discoverable: bool,
362}
363
364#[derive(Debug)]
365pub struct SocketHeader {
366 pub(crate) links: Links<SocketHeader>,
367 pub(crate) vtable: &'static SocketVTable,
368 pub(crate) key: Key,
369 pub(crate) attrs: Attributes,
370 pub(crate) port: u8,
371}
372
373#[derive(Debug, PartialEq, Eq)]
375#[non_exhaustive]
376pub enum SocketSendError {
377 NoSpace,
378 DeserFailed,
379 TypeMismatch,
380 WhatTheHell,
381}
382
383#[derive(Debug, Clone)]
384pub struct SocketVTable {
385 pub(crate) recv_owned: Option<RecvOwned>,
386 pub(crate) recv_bor: Option<RecvBorrowed>,
387 pub(crate) recv_raw: RecvRaw,
388 pub(crate) recv_err: Option<RecvError>,
389 }
393
394#[derive(Debug)]
395pub struct OwnedMessage<T: 'static> {
396 pub hdr: HeaderSeq,
397 pub t: T,
398}
399
400pub type Response<T> = Result<OwnedMessage<T>, OwnedMessage<ProtocolError>>;
401
402pub type RecvOwned = fn(
408 NonNull<()>,
410 NonNull<()>,
412 HeaderSeq,
414 &TypeId,
416) -> Result<(), SocketSendError>;
417pub type RecvBorrowed = fn(
420 NonNull<()>,
422 NonNull<()>,
424 HeaderSeq,
426) -> Result<(), SocketSendError>;
427pub type RecvRaw = fn(
430 NonNull<()>,
432 &[u8],
434 HeaderSeq,
436) -> Result<(), SocketSendError>;
437
438pub type RecvError = fn(
439 NonNull<()>,
441 HeaderSeq,
443 ProtocolError,
445);
446
447unsafe impl Linked<Links<SocketHeader>> for SocketHeader {
452 type Handle = NonNull<SocketHeader>;
453
454 fn into_ptr(r: Self::Handle) -> std::ptr::NonNull<Self> {
455 r
456 }
457
458 unsafe fn from_ptr(ptr: std::ptr::NonNull<Self>) -> Self::Handle {
459 ptr
460 }
461
462 unsafe fn links(target: NonNull<Self>) -> NonNull<Links<SocketHeader>> {
463 let node = unsafe { ptr::addr_of_mut!((*target.as_ptr()).links) };
466 unsafe { NonNull::new_unchecked(node) }
467 }
468}
469
470impl SocketSendError {
471 pub fn to_error(&self) -> ProtocolError {
472 match self {
473 SocketSendError::NoSpace => ProtocolError::SSE_NO_SPACE,
474 SocketSendError::DeserFailed => ProtocolError::SSE_DESER_FAILED,
475 SocketSendError::TypeMismatch => ProtocolError::SSE_TYPE_MISMATCH,
476 SocketSendError::WhatTheHell => ProtocolError::SSE_WHAT_THE_HELL,
477 }
478 }
479}