openthread_fuchsia/backing/
infra_if.rs

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
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::*;
use crate::{platformInfraIfInit, platformInfraIfOnReceiveIcmp6Msg};
use fuchsia_async::net::EventedFd;
use std::os::unix::io::AsRawFd;
use std::task::{Context, Poll};

pub struct OtRawSocket {
    fd: i32,
}
impl AsRawFd for OtRawSocket {
    fn as_raw_fd(&self) -> i32 {
        self.fd
    }
}

impl From<i32> for OtRawSocket {
    fn from(file_desc: i32) -> Self {
        OtRawSocket { fd: file_desc }
    }
}

pub(crate) struct InfraIfInstance {
    event_fd: Option<fasync::net::EventedFd<OtRawSocket>>,
}

impl InfraIfInstance {
    pub fn new(infra_if_idx: ot::NetifIndex) -> Option<InfraIfInstance> {
        if infra_if_idx == 0 {
            info!(tag = "infra_if", "InfraIfInstance failed to initialize: invalid infra_if_idx");
            return None;
        }
        let raw_fd_int: i32 = unsafe { platformInfraIfInit(infra_if_idx) };
        if raw_fd_int <= 0 {
            info!(tag = "infra_if", "InfraIfInstance failed to initialize: invalid raw_fd_int");
            return None;
        }
        let raw_fd: OtRawSocket = raw_fd_int.into();
        info!(tag = "infra_if", "InfraIfInstance initialized");
        Some(InfraIfInstance { event_fd: Some(unsafe { EventedFd::new(raw_fd).unwrap() }) })
    }

    pub fn poll(&self, instance: &ot::Instance, cx: &mut Context<'_>) {
        if let Some(event_fd) = self.event_fd.as_ref() {
            match event_fd.poll_readable(cx) {
                Poll::Ready(Ok(())) => unsafe {
                    platformInfraIfOnReceiveIcmp6Msg(instance.as_ot_ptr())
                },
                Poll::Ready(Err(x)) => {
                    error!(
                        tag = "infra_if",
                        "InfraIfInstance: Error poll readable from raw socket: {:?}", x
                    );
                }
                _ => {}
            }
        }
    }
}