summaryrefslogtreecommitdiff
path: root/sound_card_init/dsm/src/error.rs
blob: 4b6e8dc28ee009bda7e01719702244f230a79f9e (plain)
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
// Copyright 2020 The Chromium OS 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 std::any::Any;
use std::error;
use std::fmt;
use std::io;
use std::num::ParseIntError;
use std::path::PathBuf;
use std::sync::PoisonError;
use std::time;

use remain::sorted;

use crate::CalibData;

pub type Result<T> = std::result::Result<T, Error>;

#[sorted]
#[derive(Debug)]
pub enum Error {
    AlsaCardError(cros_alsa::CardError),
    AlsaControlError(cros_alsa::ControlError),
    AlsaControlTLVError(cros_alsa::ControlTLVError),
    CalibrationTimeout,
    CrasClientFailed(libcras::Error),
    DeserializationFailed(String, serde_yaml::Error),
    DSMParamUpdateFailed(cros_alsa::ControlTLVError),
    FileIOFailed(PathBuf, io::Error),
    InternalSpeakerNotFound,
    InvalidDatastore,
    InvalidDSMParam,
    InvalidShutDownTime,
    InvalidTemperature(f32),
    LargeCalibrationDiff(CalibData),
    MissingDSMParam,
    MutexPoisonError,
    NewPlayStreamFailed(libcras::BoxError),
    NextPlaybackBufferFailed(libcras::BoxError),
    PlaybackFailed(io::Error),
    SerdeError(PathBuf, serde_yaml::Error),
    StartPlaybackTimeout,
    SystemTimeError(time::SystemTimeError),
    UnsupportedSoundCard(String),
    VPDParseFailed(String, ParseIntError),
    WorkerPanics(Box<dyn Any + Send + 'static>),
    ZeroPlayerIsNotRunning,
    ZeroPlayerIsRunning,
}

impl PartialEq for Error {
    // Implement eq for more Error when needed.
    fn eq(&self, other: &Error) -> bool {
        match (self, other) {
            (Error::InvalidDSMParam, Error::InvalidDSMParam) => true,
            _ => false,
        }
    }
}

impl From<cros_alsa::CardError> for Error {
    fn from(err: cros_alsa::CardError) -> Error {
        Error::AlsaCardError(err)
    }
}

impl From<cros_alsa::ControlError> for Error {
    fn from(err: cros_alsa::ControlError) -> Error {
        Error::AlsaControlError(err)
    }
}

impl From<cros_alsa::ControlTLVError> for Error {
    fn from(err: cros_alsa::ControlTLVError) -> Error {
        Error::AlsaControlTLVError(err)
    }
}

impl<T> From<PoisonError<T>> for Error {
    fn from(_: PoisonError<T>) -> Error {
        Error::MutexPoisonError
    }
}

impl error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Error::*;
        match self {
            AlsaCardError(e) => write!(f, "AlsaCardError: {}", e),
            AlsaControlError(e) => write!(f, "AlsaControlError: {}", e),
            AlsaControlTLVError(e) => write!(f, "AlsaControlTLVError: {}", e),
            CalibrationTimeout => write!(f, "calibration is not finished in time"),
            DSMParamUpdateFailed(e) => write!(f, "failed to update DsmParam, err: {}", e),
            CrasClientFailed(e) => write!(f, "failed to create cras client: {}", e),
            DeserializationFailed(file_path, e) => {
                write!(f, "failed to parse {}: {}", file_path, e)
            }
            FileIOFailed(file_path, e) => write!(f, "{:#?}: {}", file_path, e),
            InvalidShutDownTime => write!(f, "invalid shutdown time"),
            InternalSpeakerNotFound => write!(f, "internal speaker is not found in cras"),
            InvalidTemperature(temp) => write!(
                f,
                "invalid calibration temperature: {}, and there is no datastore",
                temp
            ),
            InvalidDatastore => write!(f, "invalid datastore format"),
            InvalidDSMParam => write!(f, "invalid dsm param from kcontrol"),
            LargeCalibrationDiff(calib) => {
                write!(f, "calibration difference is too large, calib: {:?}", calib)
            }
            MissingDSMParam => write!(f, "missing dsm_param.bin"),
            MutexPoisonError => write!(f, "mutex is poisoned"),
            NewPlayStreamFailed(e) => write!(f, "{}", e),
            NextPlaybackBufferFailed(e) => write!(f, "{}", e),
            PlaybackFailed(e) => write!(f, "{}", e),
            SerdeError(file_path, e) => write!(f, "{:?}: {}", file_path, e),
            StartPlaybackTimeout => write!(f, "playback is not started in time"),
            SystemTimeError(e) => write!(f, "{}", e),
            UnsupportedSoundCard(name) => write!(f, "unsupported sound card: {}", name),
            VPDParseFailed(file_path, e) => write!(f, "failed to parse vpd {}: {}", file_path, e),
            WorkerPanics(e) => write!(f, "run_play_zero_worker panics: {:#?}", e),
            ZeroPlayerIsNotRunning => write!(f, "zero player is not running"),
            ZeroPlayerIsRunning => write!(f, "zero player is running"),
        }
    }
}