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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! # 网络层
//!
//! 网络层(net)规定了数据包格式(Packet),维护路由表(Protocols)

mod links;
mod packet;
mod protocols;
mod route;

use crate::link::Link;
use alloc::boxed::Box;
use links::Links;
pub use packet::Id;
use packet::{Field, Packet};
use protocols::{Protocols, RecvCallback};
use route::Routes;
use spin::RwLock;

lazy_static! {
    /// 网络层实例
    pub static ref NET: Net = Net {
        relay: RwLock::new(false),
        id: RwLock::new([0; 8]),
        routes: RwLock::new(Routes::new()),
        links: RwLock::new(Links::new()),
        protocols: RwLock::new(Protocols::new()),
    };
}
/// 上级父设备
pub const ID_PARENT: Id = [0; 8];
/// 下级子设备
pub const ID_CHILD: Id = [0xFF; 8];

/// 网络层
pub struct Net {
    /// 转发开关
    relay: RwLock<bool>,
    /// 本设备ID
    id: RwLock<Id>,
    /// 路由表
    routes: RwLock<Routes>,
    /// 链路池
    links: RwLock<Links>,
    /// 协议表
    protocols: RwLock<Protocols>,
}
impl Net {
    /// 设置本机ID
    pub fn set_id(&self, id: Id) {
        let mut myid = self.id.write();
        *myid = id;
    }
    /// 设置中继开关
    pub fn set_relay(&self, relay: bool) {
        let mut myrelay = self.relay.write();
        *myrelay = relay;
    }
    /// 添加本地链路
    pub fn add_link(&self, link: Link, is_parent: bool) {
        link.set_recv_cb(Box::new(move |link: Link, load: &[u8]| {
            NET.recv_handle(link, load);
        }));
        let mut links = self.links.write();
        links.add(link, is_parent);
    }
    /// 注册子协议的数据包接收函数
    pub fn register_protocol(&self, id: u8, cb: Box<RecvCallback>) {
        let mut protocols = self.protocols.write();
        protocols.add_protocol(id, cb);
    }
    /// 通用发送函数
    fn send_common(
        &self,
        packet: &mut Packet,
        dst: Option<&Id>,
        dst_link: Option<&Link>,
        origin: Option<&Link>,
    ) {
        let mut link: Option<Link> = None;

        let id: &Id = if let Some(id) = dst {
            packet.set_var(Field::Dst, id);
            id
        } else {
            &packet.dst
        };
        if let None = dst_link {
            let routes = self.routes.read();
            if let Some(l) = routes.find(id) {
                link = Some(l);
            }
        }
        let buf = packet.to_buf();
        if let Some(link) = link {
            if let Some(origin) = origin {
                if origin.eq(&link) {
                    return;
                }
            }
            link.send(&buf);
        } else {
            if let Some(origin) = origin {
                for parent in &self.links.read().parents {
                    if origin.eq(&parent) {
                        return;
                    }
                }
            }

            for parent in &self.links.read().parents {
                parent.send(&buf);
            }
        }
    }
    /// 发送数据包到链路接口
    pub fn send(&self, protocol: u8, data: &[u8], id: Option<&Id>, link: Option<&Link>) {
        let mut packet = Packet::new();
        packet.set_var(Field::Protocol, &[protocol]);
        packet.set_var(Field::Data, data);
        packet.set_var(Field::Src, &*self.id.read());
        packet.set_var(Field::Ttl, &[8]);
        self.send_common(&mut packet, id, link, None);
    }
    /// 发送数据包到所有子设备
    pub fn broadcast(&self, protocol: u8, data: &[u8]) {
        let mut packet = Packet::new();
        packet.set_var(Field::Protocol, &[protocol]);
        packet.set_var(Field::Data, data);
        packet.set_var(Field::Src, &*self.id.read());
        packet.set_var(Field::Dst, &ID_CHILD);
        packet.set_var(Field::Ttl, &[0]);
        let buf = packet.to_buf();
        let routes = self.routes.read();
        for (_id, route) in routes.iter() {
            route.link.send(&buf);
        }
    }
    /// 收到了数据包
    fn recv_handle(&self, link: Link, buf: &[u8]) {
        let packet = Packet::try_from_buf(buf);
        let mut packet = if let Ok(packet) = packet {
            packet
        } else {
            return;
        };
        self.update_route(&packet.src, &link);

        if packet.dst == *self.id.read() {
            let protocols = self.protocols.read();
            protocols.distribute(packet.protocol, &packet.src, &packet.data);
        } else if *self.relay.read() {
            if packet.ttl > 0 {
                packet.set_var(Field::Ttl, &[packet.ttl - 1]);
                self.send_common(&mut packet, None, None, Some(&link));
            } else {
                // I:("unmp TTL is 0.");
            }
        }
    }
    /// 设置固定路由
    pub fn connect(&self, id: &Id, link: &Link) {
        let mut routes = self.routes.write();
        routes.remove(id);
        routes.update(id, link.clone());
        routes.fix(id);
    }
    /// 更新路由
    pub fn update_route(&self, id: &Id, link: &Link) {
        for parent in &self.links.read().parents {
            if link.eq(&parent) {
                return;
            }
        }
        let mut routes = self.routes.write();
        let route = routes.find(id);
        if let Some(route) = route {
            if route.uid() != link.uid() {
                routes.update(id, link.clone());
            }
        } else {
            routes.update(id, link.clone());
        }
    }
}