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
//! 客户端

#![no_std]
extern crate alloc;

use alloc::format;
use alloc::sync::Arc;
use embedded_hal::serial;
use nb::block;
use spin::Mutex;
use unmp::link::{Driver, Link};

/// UDP客户端链路驱动接口
struct Client<T: serial::Write<u8> + Send + 'static> {
    //name: String,
    serial: Mutex<T>,
}
impl<T> Driver for Client<T>
where
    T: serial::Write<u8> + Send + 'static,
{
    fn send(&self, buf: &[u8]) {
        //println!("send: {:02X?}", buf);
        let mut serial = self.serial.lock();
        for res in buf {
            block!(serial.write(*res)).ok();
        }
    }
}

/// 创建一个UDP链路实例,并连接到指定地址端口
pub fn start<T>(name: &str, serial: T) -> Link
where
    T: serial::Write<u8> + Send + 'static,
{
    let driver = Arc::new(Client {
        serial: Mutex::new(serial),
    });
    let link = Link::new(&format!("Serial({})", name), driver);
    return link;
}