netmod_wd/
lib.rs

1//! Netmod driver for Android WiFi Direct
2
3use async_std::{
4    channel::{bounded, Receiver, Sender},
5    sync::Arc,
6    task,
7};
8use async_trait::async_trait;
9use netmod::{Endpoint, Frame, Result, Target};
10
11pub struct WdMod {
12    recv_queue: (Sender<(Frame, Target)>, Receiver<(Frame, Target)>),
13    send_queue: (Sender<(Frame, Target)>, Receiver<(Frame, Target)>),
14}
15
16impl WdMod {
17    pub fn new() -> Arc<Self> {
18        Arc::new(Self {
19            recv_queue: bounded(1),
20            send_queue: bounded(1),
21        })
22    }
23
24    /// Give some data to this netmod, receiving it on the device
25    ///
26    /// This function is called by the java-android driver stack in
27    /// android-support which is called by any app that implements the
28    /// WifiDirect mode, It could also be used as a general FFI shim
29    /// for other drivers.
30    pub fn give(self: &Arc<Self>, f: Frame, t: Target) {
31        let this = Arc::clone(self);
32        task::spawn(async move { this.recv_queue.0.send((f, t)).await.unwrap() });
33    }
34
35    /// Block on taking a new
36    pub fn take(self: &Arc<Self>) -> (Frame, Target) {
37        task::block_on(async { self.send_queue.1.recv().await.unwrap() })
38    }
39}
40
41#[async_trait]
42impl Endpoint for WdMod {
43    fn size_hint(&self) -> usize {
44        0
45    }
46
47    async fn send(&self, frame: Frame, t: Target) -> Result<()> {
48        self.send_queue.0.send((frame, t)).await.unwrap();
49        Ok(())
50    }
51
52    async fn next(&self) -> Result<(Frame, Target)> {
53        Ok(self.recv_queue.1.recv().await.unwrap())
54    }
55}