diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 12:00:15 +0100 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 12:06:23 +0100 |
commit | 73c8aebac338d3454d7e345ffd687324317077ec (patch) | |
tree | 0dd468f41257d3c6c9287230792ff443657aed54 | |
parent | 6142752da1563c1ab873dc7069aeec72522cca99 (diff) | |
download | nitrokey-rs-73c8aebac338d3454d7e345ffd687324317077ec.tar.gz nitrokey-rs-73c8aebac338d3454d7e345ffd687324317077ec.tar.bz2 |
Add the get_sd_card_usage function to the Storage struct
This patch adds support for the NK_get_SD_usage_data function. It
returns a range of the SD card that has not been accessed during this
power cycle.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | TODO.md | 1 | ||||
-rw-r--r-- | src/device/storage.rs | 42 | ||||
-rw-r--r-- | tests/device.rs | 10 |
4 files changed, 51 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b477c3b..cebe8cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ SPDX-License-Identifier: CC0-1.0 - Add the `connect_path` function to the `Manager` struct. - Add the `get_status` function to the `Device` trait. - Rename `Status::get_status` to `get_storage_status`. +- Add the `get_sd_card_usage` function to the `Storage` struct. # v0.4.0 (2020-01-02) - Remove the `test-pro` and `test-storage` features. @@ -5,7 +5,6 @@ SPDX-License-Identifier: CC0-1.0 - Add support for the currently unsupported commands: - `NK_fill_SD_card_with_random_data` - - `NK_get_SD_usage_data` - `NK_get_progress_bar_value` - Clear passwords from memory. - Lock password safe in `PasswordSafe::drop()` (see [nitrokey-storage-firmware diff --git a/src/device/storage.rs b/src/device/storage.rs index 08ec4e9..a2c598b 100644 --- a/src/device/storage.rs +++ b/src/device/storage.rs @@ -1,7 +1,8 @@ -// Copyright (C) 2019-2019 Robin Krahl <robin.krahl@ireas.org> +// Copyright (C) 2019-2020 Robin Krahl <robin.krahl@ireas.org> // SPDX-License-Identifier: MIT use std::fmt; +use std::ops; use nitrokey_sys; @@ -635,6 +636,45 @@ impl<'a> Storage<'a> { }) } + /// Returns a range of the SD card that has not been used to during this power cycle. + /// + /// The Nitrokey Storage tracks read and write access to the SD card during a power cycle. + /// This method returns a range of the SD card that has not been accessed during this power + /// cycle. The range is relative to the total size of the SD card, so both values are less + /// than or equal to 100. This can be used as a guideline when creating a hidden volume. + /// + /// # Example + /// + /// ```no_run + /// let mut manager = nitrokey::take()?; + /// let storage = manager.connect_storage()?; + /// let usage = storage.get_sd_card_usage()?; + /// println!("SD card usage: {}..{}", usage.start, usage.end); + /// # Ok::<(), nitrokey::Error>(()) + /// ``` + pub fn get_sd_card_usage(&self) -> Result<ops::Range<u8>, Error> { + let mut usage_data = nitrokey_sys::NK_SD_usage_data { + write_level_min: 0, + write_level_max: 0, + }; + let result = unsafe { nitrokey_sys::NK_get_SD_usage_data(&mut usage_data) }; + match get_command_result(result) { + Ok(_) => { + if usage_data.write_level_min > usage_data.write_level_max + || usage_data.write_level_max > 100 + { + Err(Error::UnexpectedError) + } else { + Ok(ops::Range { + start: usage_data.write_level_min, + end: usage_data.write_level_max, + }) + } + } + Err(err) => Err(err), + } + } + /// Blinks the red and green LED alternatively and infinitely until the device is reconnected. pub fn wink(&mut self) -> Result<(), Error> { get_command_result(unsafe { nitrokey_sys::NK_wink() }) diff --git a/tests/device.rs b/tests/device.rs index f2a7031..070f3c1 100644 --- a/tests/device.rs +++ b/tests/device.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2018-2019 Robin Krahl <robin.krahl@ireas.org> +// Copyright (C) 2018-2020 Robin Krahl <robin.krahl@ireas.org> // SPDX-License-Identifier: MIT mod util; @@ -640,6 +640,14 @@ fn clear_new_sd_card_warning(device: Storage) { } #[test_device] +fn get_sd_card_usage(device: Storage) { + let range = unwrap_ok!(device.get_sd_card_usage()); + + assert!(range.end >= range.start); + assert!(range.end <= 100); +} + +#[test_device] fn export_firmware(device: Storage) { let mut device = device; assert_cmd_err!( |