scmi: Add sensor device initialization function

Implementation accessing real sensors will need to set up the device
instance.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
This commit is contained in:
Milan Zamazal 2023-08-05 17:42:17 +02:00 committed by Alex Bennée
parent cbb0449d43
commit 789288c372
3 changed files with 20 additions and 1 deletions

View File

@ -191,6 +191,10 @@ pub trait SensorT: Send {
fn sensor(&self) -> &Sensor;
fn sensor_mut(&mut self) -> &mut Sensor;
fn initialize(&mut self) -> Result<(), DeviceError> {
Ok(())
}
fn protocol(&self) -> ProtocolId {
SENSOR_PROTOCOL_ID
}
@ -328,6 +332,10 @@ pub trait SensorT: Send {
pub struct SensorDevice(pub(crate) Box<dyn SensorT>);
impl ScmiDevice for SensorDevice {
fn initialize(&mut self) -> Result<(), DeviceError> {
self.0.initialize()
}
fn protocol(&self) -> ProtocolId {
self.0.protocol()
}

View File

@ -11,6 +11,8 @@ use itertools::Itertools;
use log::{debug, error, info};
use thiserror::Error as ThisError;
use crate::devices::common::DeviceError;
pub type MessageHeader = u32;
pub const MAX_SIMPLE_STRING_LENGTH: usize = 16; // incl. NULL terminator
@ -484,6 +486,7 @@ pub enum ScmiDeviceError {
}
pub trait ScmiDevice: Send {
fn initialize(&mut self) -> Result<(), DeviceError>;
fn protocol(&self) -> ProtocolId;
fn handle(
&mut self,

View File

@ -103,7 +103,15 @@ impl VuScmiBackend {
}
match device_mapping.get(name.as_str()) {
Some(specification) => match (specification.constructor)(properties) {
Ok(device) => handler.register_device(device),
Ok(mut device) => {
if let Err(error) = device.initialize() {
return Result::Err(VuScmiError::DeviceConfigurationError(
name.clone(),
error,
));
}
handler.register_device(device);
}
Err(error) => {
return Result::Err(VuScmiError::DeviceConfigurationError(
name.clone(),