Skip to main content

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    /// Functional equivalent of
36    /// [`otsys::otPlatRadioGetCslAccuracy`](crate::otsys::otPlatRadioGetCslAccuracy).
37    fn get_csl_accuracy(&self) -> u8;
38
39    /// Functional equivalent of
40    /// [`otsys::otPlatRadioGetCslUncertainty`](crate::otsys::otPlatRadioGetCslUncertainty).
41    fn get_csl_uncertainty(&self) -> u8;
42
43    /// Calling into OT state change handler in platform radio
44    fn on_radio_handle_state_change(&self, flags: ot::ChangedFlags);
45}
46
47impl<T: Radio + Boxable> Radio for ot::Box<T> {
48    fn get_coex_metrics(&self) -> Result<RadioCoexMetrics> {
49        self.as_ref().get_coex_metrics()
50    }
51
52    fn get_rssi(&self) -> Decibels {
53        self.as_ref().get_rssi()
54    }
55
56    fn get_region(&self) -> Result<RadioRegion> {
57        self.as_ref().get_region()
58    }
59
60    fn set_region(&self, region: RadioRegion) -> Result {
61        self.as_ref().set_region(region)
62    }
63
64    fn get_transmit_power(&self) -> Result<Decibels> {
65        self.as_ref().get_transmit_power()
66    }
67
68    fn radio_get_version_string(&self) -> &str {
69        self.as_ref().radio_get_version_string()
70    }
71
72    fn get_csl_accuracy(&self) -> u8 {
73        self.as_ref().get_csl_accuracy()
74    }
75
76    fn get_csl_uncertainty(&self) -> u8 {
77        self.as_ref().get_csl_uncertainty()
78    }
79
80    fn on_radio_handle_state_change(&self, flags: ot::ChangedFlags) {
81        self.as_ref().on_radio_handle_state_change(flags)
82    }
83}
84
85impl Radio for Instance {
86    fn get_coex_metrics(&self) -> Result<RadioCoexMetrics> {
87        let mut ret = RadioCoexMetrics::default();
88        Error::from(unsafe { otPlatRadioGetCoexMetrics(self.as_ot_ptr(), ret.as_ot_mut_ptr()) })
89            .into_result()?;
90        Ok(ret)
91    }
92
93    fn get_rssi(&self) -> Decibels {
94        unsafe { otPlatRadioGetRssi(self.as_ot_ptr()) }
95    }
96
97    fn get_region(&self) -> Result<RadioRegion> {
98        let mut ret = 0u16;
99        Error::from(unsafe { otPlatRadioGetRegion(self.as_ot_ptr(), &mut ret as *mut u16) })
100            .into_result()?;
101        Ok(ret.into())
102    }
103
104    fn set_region(&self, region: RadioRegion) -> Result {
105        Error::from(unsafe { otPlatRadioSetRegion(self.as_ot_ptr(), region.into()) }).into()
106    }
107
108    fn get_transmit_power(&self) -> Result<Decibels> {
109        let mut ret = DECIBELS_UNSPECIFIED;
110        Error::from(unsafe {
111            otPlatRadioGetTransmitPower(self.as_ot_ptr(), &mut ret as *mut Decibels)
112        })
113        .into_result()?;
114        Ok(ret)
115    }
116
117    fn radio_get_version_string(&self) -> &str {
118        unsafe {
119            // SAFETY: `otPlatRadioGetVersionString` guarantees to return a C-String that will not
120            //         change.
121            CStr::from_ptr(otPlatRadioGetVersionString(self.as_ot_ptr()))
122                .to_str()
123                .expect("OpenThread version string was bad UTF8")
124        }
125    }
126
127    fn get_csl_accuracy(&self) -> u8 {
128        unsafe { otPlatRadioGetCslAccuracy(self.as_ot_ptr()) }
129    }
130
131    fn get_csl_uncertainty(&self) -> u8 {
132        unsafe { otPlatRadioGetCslUncertainty(self.as_ot_ptr()) }
133    }
134
135    fn on_radio_handle_state_change(&self, flags: ot::ChangedFlags) {
136        unsafe { platformRadioHandleStateChange(self.as_ot_ptr(), flags.bits()) }
137    }
138}