openthread/ot/
radio.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::prelude_internal::*;
6
7/// Methods from the [OpenThread "Radio" Module][1].
8///
9/// [1]: https://openthread.io/reference/group/radio-operation
10pub trait Radio {
11    /// Functional equivalent of
12    /// [`otsys::otPlatRadioGetCoexMetrics`](crate::otsys::otPlatRadioGetCoexMetrics).
13    fn get_coex_metrics(&self) -> Result<RadioCoexMetrics>;
14
15    /// Functional equivalent of
16    /// [`otsys::otPlatRadioGetRssi`](crate::otsys::otPlatRadioGetRssi).
17    fn get_rssi(&self) -> Decibels;
18
19    /// Functional equivalent of
20    /// [`otsys::otPlatRadioGetRegion`](crate::otsys::otPlatRadioGetRegion).
21    fn get_region(&self) -> Result<RadioRegion>;
22
23    /// Functional equivalent of
24    /// [`otsys::otPlatRadioSetRegion`](crate::otsys::otPlatRadioSetRegion).
25    fn set_region(&self, region: RadioRegion) -> Result;
26
27    /// Functional equivalent of
28    /// [`otsys::otPlatRadioGetTransmitPower`](crate::otsys::otPlatRadioGetTransmitPower).
29    fn get_transmit_power(&self) -> Result<Decibels>;
30
31    /// Functional equivalent of
32    /// [`otsys::otPlatRadioGetVersionString`](crate::otsys::otPlatRadioGetVersionString).
33    fn radio_get_version_string(&self) -> &str;
34
35    /// Calling into OT state change handler in platform radio
36    fn on_radio_handle_state_change(&self, flags: ot::ChangedFlags);
37}
38
39impl<T: Radio + Boxable> Radio for ot::Box<T> {
40    fn get_coex_metrics(&self) -> Result<RadioCoexMetrics> {
41        self.as_ref().get_coex_metrics()
42    }
43
44    fn get_rssi(&self) -> Decibels {
45        self.as_ref().get_rssi()
46    }
47
48    fn get_region(&self) -> Result<RadioRegion> {
49        self.as_ref().get_region()
50    }
51
52    fn set_region(&self, region: RadioRegion) -> Result {
53        self.as_ref().set_region(region)
54    }
55
56    fn get_transmit_power(&self) -> Result<Decibels> {
57        self.as_ref().get_transmit_power()
58    }
59
60    fn radio_get_version_string(&self) -> &str {
61        self.as_ref().radio_get_version_string()
62    }
63
64    fn on_radio_handle_state_change(&self, flags: ot::ChangedFlags) {
65        self.as_ref().on_radio_handle_state_change(flags)
66    }
67}
68
69impl Radio for Instance {
70    fn get_coex_metrics(&self) -> Result<RadioCoexMetrics> {
71        let mut ret = RadioCoexMetrics::default();
72        Error::from(unsafe { otPlatRadioGetCoexMetrics(self.as_ot_ptr(), ret.as_ot_mut_ptr()) })
73            .into_result()?;
74        Ok(ret)
75    }
76
77    fn get_rssi(&self) -> Decibels {
78        unsafe { otPlatRadioGetRssi(self.as_ot_ptr()) }
79    }
80
81    fn get_region(&self) -> Result<RadioRegion> {
82        let mut ret = 0u16;
83        Error::from(unsafe { otPlatRadioGetRegion(self.as_ot_ptr(), &mut ret as *mut u16) })
84            .into_result()?;
85        Ok(ret.into())
86    }
87
88    fn set_region(&self, region: RadioRegion) -> Result {
89        Error::from(unsafe { otPlatRadioSetRegion(self.as_ot_ptr(), region.into()) }).into()
90    }
91
92    fn get_transmit_power(&self) -> Result<Decibels> {
93        let mut ret = DECIBELS_UNSPECIFIED;
94        Error::from(unsafe {
95            otPlatRadioGetTransmitPower(self.as_ot_ptr(), &mut ret as *mut Decibels)
96        })
97        .into_result()?;
98        Ok(ret)
99    }
100
101    fn radio_get_version_string(&self) -> &str {
102        unsafe {
103            // SAFETY: `otPlatRadioGetVersionString` guarantees to return a C-String that will not
104            //         change.
105            CStr::from_ptr(otPlatRadioGetVersionString(self.as_ot_ptr()))
106                .to_str()
107                .expect("OpenThread version string was bad UTF8")
108        }
109    }
110
111    fn on_radio_handle_state_change(&self, flags: ot::ChangedFlags) {
112        unsafe { platformRadioHandleStateChange(self.as_ot_ptr(), flags.bits()) }
113    }
114}