|
| 1 | +//! Updates tags from the NVS (default partition). |
| 2 | +//! |
| 3 | +//! Note that this module exposes two separate set of APIs: |
| 4 | +//! * the get_XXX/set_XXX API (where XXX is u8, str, etc.) - this is only for interop with C code that uses the C ESP IDF NVS API as well. |
| 5 | +//! * the `get_raw`/`set_raw` APIs that take a `&[u8]`. This is the "native" Rust API that implements the `RawStorage` trait from `embedded-svc` and it should be preferred actually, as you can layer on top of it any serde you want. |
| 6 | +//! |
| 7 | +//! More info reagarding NVS: |
| 8 | +//! https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html |
| 9 | +
|
| 10 | +use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported |
| 11 | + |
| 12 | +use esp_idf_svc::log::EspLogger; |
| 13 | +use esp_idf_svc::nvs::*; |
| 14 | +use log::info; |
| 15 | + |
| 16 | +fn main() -> anyhow::Result<()> { |
| 17 | + EspLogger::initialize_default(); |
| 18 | + |
| 19 | + let nvs_default_partition: EspNvsPartition<NvsDefault> = |
| 20 | + EspDefaultNvsPartition::take().unwrap(); |
| 21 | + |
| 22 | + let test_namespace = "test_ns"; |
| 23 | + let mut nvs = match EspNvs::new(nvs_default_partition, test_namespace, true) { |
| 24 | + Ok(nvs) => { |
| 25 | + info!("Got namespace {:?} from default partition", test_namespace); |
| 26 | + nvs |
| 27 | + } |
| 28 | + Err(e) => panic!("Could't get namespace {:?}", e), |
| 29 | + }; |
| 30 | + |
| 31 | + let tag_u8 = "test_u8"; |
| 32 | + |
| 33 | + match nvs.set_u8(tag_u8, 42) { |
| 34 | + Ok(_) => info!("Tag updated"), |
| 35 | + // You can find the meaning of the error codes in the output of the error branch in: |
| 36 | + // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/error-codes.html |
| 37 | + Err(e) => info!("Tag not updated {:?}", e), |
| 38 | + }; |
| 39 | + |
| 40 | + match nvs.get_u8(tag_u8).unwrap() { |
| 41 | + Some(v) => info!("{:?} = {:?}", tag_u8, v), |
| 42 | + None => info!("{:?} not found", tag_u8), |
| 43 | + }; |
| 44 | + |
| 45 | + let tag_test_str = "test_str"; |
| 46 | + // String values are limited in the IDF to 4000 bytes, but our buffer is shorter. |
| 47 | + const MAX_STR_LEN: usize = 100; |
| 48 | + |
| 49 | + let the_str_len: usize = nvs.str_len(tag_test_str).map_or(0, |v| { |
| 50 | + info!("Got stored string length of {:?}", v); |
| 51 | + let vv = v.unwrap_or(0); |
| 52 | + if vv >= MAX_STR_LEN { |
| 53 | + info!("Too long, trimming"); |
| 54 | + 0 |
| 55 | + } else { |
| 56 | + vv |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + match the_str_len == 0 { |
| 61 | + true => info!("{:?} does not seem to exist", tag_test_str), |
| 62 | + false => { |
| 63 | + let mut buffer: [u8; MAX_STR_LEN] = [0; MAX_STR_LEN]; |
| 64 | + match nvs.get_str(tag_test_str, &mut buffer).unwrap() { |
| 65 | + Some(v) => info!("{:?} = {:?}", tag_test_str, v), |
| 66 | + None => info!("We got nothing from {:?}", tag_test_str), |
| 67 | + }; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + match nvs.set_str(tag_test_str, "Hello from the NVS!") { |
| 72 | + Ok(_) => info!("{:?} updated", tag_test_str), |
| 73 | + Err(e) => info!("{:?} not updated {:?}", tag_test_str, e), |
| 74 | + }; |
| 75 | + |
| 76 | + Ok(()) |
| 77 | +} |
0 commit comments