summaryrefslogtreecommitdiff
path: root/sound_card_init/max98390d/src/datastore.rs
blob: 12ebff73132fb172f9dc468b030f08122c907910 (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
// 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::fs::File;
use std::io::{prelude::*, BufReader, BufWriter};
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use sys_util::info;
use utils::DATASTORE_DIR;

use crate::error::{Error, Result};

/// `Datastore`, which stores and reads calibration values in yaml format.
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub enum Datastore {
    /// Indicates using values in VPD.
    UseVPD,
    /// rdc is (11 / 3) / actual_rdc * 2^20.
    /// ambient_temp is actual_temp * 2^12 / 100.
    DSM { rdc: i32, ambient_temp: i32 },
}

impl Datastore {
    /// Creates a `Datastore` and initializes its fields from DATASTORE_DIR/<snd_card>/{file}.
    pub fn from_file(snd_card: &str, file: &str) -> Result<Datastore> {
        let path = PathBuf::from(DATASTORE_DIR).join(snd_card).join(file);

        let io_err = |e| Error::FileIOFailed(path.to_string_lossy().to_string(), e);
        let parse_err = |e: serde_yaml::Error| {
            Error::DeserializationFailed(path.to_string_lossy().to_string(), e)
        };

        let reader = BufReader::new(File::open(&path).map_err(io_err)?);
        let datastore: Datastore = serde_yaml::from_reader(reader).map_err(parse_err)?;
        Ok(datastore)
    }

    /// Saves a `Datastore` to DATASTORE_DIR/<snd_card>/{file}.
    pub fn save(&self, snd_card: &str, file: &str) -> Result<()> {
        let path = PathBuf::from(DATASTORE_DIR).join(snd_card).join(file);
        let io_err = |e| Error::FileIOFailed(path.to_string_lossy().to_string(), e);

        let mut writer = BufWriter::new(File::create(&path).map_err(io_err)?);
        writer
            .write(
                serde_yaml::to_string(self)
                    .map_err(Error::SerializationFailed)?
                    .as_bytes(),
            )
            .map_err(io_err)?;
        writer.flush().map_err(io_err)?;
        info!("update Datastore {}: {:?}", path.to_string_lossy(), self);
        Ok(())
    }
}