Skip to content

Commit b505645

Browse files
committed
cid tweaks, tests
1 parent 1604b65 commit b505645

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

src/sdcard/cid.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ pub struct Cid {
1717
manufacturer_id: u8,
1818
/// OID.
1919
#[bits(104..=119, rw)]
20-
application_id: u16,
20+
oem_id: u16,
2121
/// PVM.
2222
#[bits(64..=103, rw)]
23-
product_name: u40,
23+
product_name_raw: u40,
2424
/// PSN.
2525
#[bits(24..=55, rw)]
2626
product_serial_number: u32,
2727
/// MDT.
2828
#[bits(8..=19, rw)]
29-
manufacturing_data: u12,
29+
manufacturing_date: u12,
3030
/// CRC.
3131
#[bits(1..=7, rw)]
3232
crc7: u7,
@@ -53,4 +53,56 @@ impl Cid {
5353
let calculated = crc7(&raw_bytes[0..15]);
5454
self.crc7().value() == calculated
5555
}
56+
57+
/// Product name as a byte array.
58+
#[inline]
59+
pub fn product_name_bytes(&self) -> [u8; 5] {
60+
self.product_name_raw().to_be_bytes()
61+
}
62+
}
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
68+
#[test]
69+
fn basic_test() {
70+
// CID retrieved from a real SD card.
71+
let raw_cid = [
72+
0x12, 0x34, 0x56, 0x41, 0x53, 0x54, 0x43, 0x0, 0x20, 0x0, 0x0, 0xc, 0xef, 0x1, 0x65,
73+
0xef,
74+
];
75+
// This also verifies the checksum.
76+
let cid = Cid::new(&raw_cid);
77+
assert!(cid.is_ok());
78+
79+
let cid = cid.unwrap();
80+
// https://www.bahjeez.com/sd-card-manufacturer-ids/: Patriot.
81+
assert_eq!(cid.manufacturer_id(), 0x12);
82+
assert_eq!(cid.oem_id(), 0x3456);
83+
84+
let product_name_raw = cid.product_name_bytes();
85+
let product_name = core::str::from_utf8(&product_name_raw).unwrap();
86+
assert_eq!(product_name, "ASTC\0");
87+
}
88+
89+
#[test]
90+
fn basic_test_unchecked() {
91+
// CID retrieved from a real SD card.
92+
let raw_cid = [
93+
0x12, 0x34, 0x56, 0x41, 0x53, 0x54, 0x43, 0x0, 0x20, 0x0, 0x0, 0xc, 0xef, 0x1, 0x65,
94+
0x00,
95+
];
96+
// Invalid checksum ignored.
97+
let cid = Cid::new_unchecked(&raw_cid);
98+
assert!(!cid.verify_crc7());
99+
100+
// https://www.bahjeez.com/sd-card-manufacturer-ids/: Patriot.
101+
assert_eq!(cid.manufacturer_id(), 0x12);
102+
assert_eq!(cid.oem_id(), 0x3456);
103+
104+
let product_name_raw = cid.product_name_bytes();
105+
let product_name = core::str::from_utf8(&product_name_raw).unwrap();
106+
assert_eq!(product_name, "ASTC\0");
107+
}
56108
}

0 commit comments

Comments
 (0)