mirror of https://github.com/eldruin/ds323x-rs
Limit setting year to 20th century.
parent
7070810621
commit
29084a7b9d
|
|
@ -36,10 +36,9 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_datetime(&mut self, datetime: &NaiveDateTime) -> Result<(), Self::Error> {
|
fn set_datetime(&mut self, datetime: &NaiveDateTime) -> Result<(), Self::Error> {
|
||||||
if datetime.year() < 2000 || datetime.year() > 2100 {
|
if !(2000..=2099).contains(&datetime.year()) {
|
||||||
return Err(Error::InvalidInputData);
|
return Err(Error::InvalidInputData);
|
||||||
}
|
}
|
||||||
let (month, year) = month_year_to_registers(datetime.month() as u8, datetime.year() as u16);
|
|
||||||
let mut payload = [
|
let mut payload = [
|
||||||
Register::SECONDS,
|
Register::SECONDS,
|
||||||
decimal_to_packed_bcd(datetime.second() as u8),
|
decimal_to_packed_bcd(datetime.second() as u8),
|
||||||
|
|
@ -47,8 +46,8 @@ where
|
||||||
hours_to_register(Hours::H24(datetime.hour() as u8))?,
|
hours_to_register(Hours::H24(datetime.hour() as u8))?,
|
||||||
datetime.weekday().number_from_sunday() as u8,
|
datetime.weekday().number_from_sunday() as u8,
|
||||||
decimal_to_packed_bcd(datetime.day() as u8),
|
decimal_to_packed_bcd(datetime.day() as u8),
|
||||||
month,
|
decimal_to_packed_bcd(datetime.month() as u8),
|
||||||
year,
|
decimal_to_packed_bcd((datetime.year() - 2000) as u8),
|
||||||
];
|
];
|
||||||
self.iface.write_data(&mut payload)
|
self.iface.write_data(&mut payload)
|
||||||
}
|
}
|
||||||
|
|
@ -174,39 +173,30 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_year(&mut self, year: u16) -> Result<(), Self::Error> {
|
fn set_year(&mut self, year: u16) -> Result<(), Self::Error> {
|
||||||
if !(2000..=2100).contains(&year) {
|
if !(2000..=2099).contains(&year) {
|
||||||
return Err(Error::InvalidInputData);
|
return Err(Error::InvalidInputData);
|
||||||
}
|
}
|
||||||
let data = self.iface.read_register(Register::MONTH)?;
|
let data = self.iface.read_register(Register::MONTH)?;
|
||||||
let month_bcd = data & !BitFlags::CENTURY;
|
let month_bcd = data & !BitFlags::CENTURY;
|
||||||
if year > 2099 {
|
|
||||||
let mut data = [
|
let mut data = [
|
||||||
Register::MONTH,
|
Register::MONTH,
|
||||||
BitFlags::CENTURY | month_bcd,
|
month_bcd,
|
||||||
decimal_to_packed_bcd((year - 2100) as u8),
|
decimal_to_packed_bcd((year - 2000) as u8),
|
||||||
];
|
];
|
||||||
self.iface.write_data(&mut data)
|
self.iface.write_data(&mut data)
|
||||||
} else {
|
|
||||||
let mut data = [
|
|
||||||
Register::MONTH,
|
|
||||||
month_bcd,
|
|
||||||
decimal_to_packed_bcd((year - 2000) as u8),
|
|
||||||
];
|
|
||||||
self.iface.write_data(&mut data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_date(&mut self, date: &rtcc::NaiveDate) -> Result<(), Self::Error> {
|
fn set_date(&mut self, date: &rtcc::NaiveDate) -> Result<(), Self::Error> {
|
||||||
if date.year() < 2000 || date.year() > 2100 {
|
if !(2000..=2099).contains(&date.year()) {
|
||||||
return Err(Error::InvalidInputData);
|
return Err(Error::InvalidInputData);
|
||||||
}
|
}
|
||||||
let (month, year) = month_year_to_registers(date.month() as u8, date.year() as u16);
|
|
||||||
let mut payload = [
|
let mut payload = [
|
||||||
Register::DOW,
|
Register::DOW,
|
||||||
date.weekday().number_from_sunday() as u8,
|
date.weekday().number_from_sunday() as u8,
|
||||||
decimal_to_packed_bcd(date.day() as u8),
|
decimal_to_packed_bcd(date.day() as u8),
|
||||||
month,
|
decimal_to_packed_bcd(date.month() as u8),
|
||||||
year,
|
decimal_to_packed_bcd((date.year() - 2000) as u8),
|
||||||
];
|
];
|
||||||
self.iface.write_data(&mut payload)
|
self.iface.write_data(&mut payload)
|
||||||
}
|
}
|
||||||
|
|
@ -251,18 +241,6 @@ fn year_from_registers(month: u8, year: u8) -> u16 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn month_year_to_registers(month: u8, year: u16) -> (u8, u8) {
|
|
||||||
if year > 2099 {
|
|
||||||
let month = BitFlags::CENTURY | decimal_to_packed_bcd(month);
|
|
||||||
(month, decimal_to_packed_bcd((year - 2100) as u8))
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
decimal_to_packed_bcd(month),
|
|
||||||
decimal_to_packed_bcd((year - 2000) as u8),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_24h_format(hours_data: u8) -> bool {
|
fn is_24h_format(hours_data: u8) -> bool {
|
||||||
hours_data & BitFlags::H24_H12 == 0
|
hours_data & BitFlags::H24_H12 == 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,18 +206,7 @@ mod year {
|
||||||
0b1001_1001
|
0b1001_1001
|
||||||
);
|
);
|
||||||
|
|
||||||
get_param_read_array_test!(century1_get, year, 2100, MONTH, [0b1000_0000, 0], [0, 0]);
|
set_invalid_param_range_test!(invalid, set_year, 1999, 2100);
|
||||||
read_set_param_write_two_test!(
|
|
||||||
century1_set,
|
|
||||||
set_year,
|
|
||||||
2100,
|
|
||||||
MONTH,
|
|
||||||
0b0001_0010,
|
|
||||||
0b1001_0010,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
set_invalid_param_range_test!(invalid, set_year, 1999, 2101);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! invalid_dt_test {
|
macro_rules! invalid_dt_test {
|
||||||
|
|
@ -233,7 +222,7 @@ macro_rules! invalid_dt_test {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn datetime_too_big() {
|
fn datetime_too_big() {
|
||||||
let dt = new_datetime(2101, 1, 2, 3, 4, 5);
|
let dt = new_datetime(2100, 1, 2, 3, 4, 5);
|
||||||
let mut dev = $create_method(&[]);
|
let mut dev = $create_method(&[]);
|
||||||
assert_invalid_input_data!(dev.set_datetime(&dt));
|
assert_invalid_input_data!(dev.set_datetime(&dt));
|
||||||
$destroy_method(dev);
|
$destroy_method(dev);
|
||||||
|
|
@ -247,7 +236,7 @@ macro_rules! invalid_dt_test {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn date_too_big() {
|
fn date_too_big() {
|
||||||
let d = new_date(2101, 1, 2);
|
let d = new_date(2100, 1, 2);
|
||||||
let mut dev = $create_method(&[]);
|
let mut dev = $create_method(&[]);
|
||||||
assert_invalid_input_data!(dev.set_date(&d));
|
assert_invalid_input_data!(dev.set_date(&d));
|
||||||
$destroy_method(dev);
|
$destroy_method(dev);
|
||||||
|
|
@ -339,17 +328,6 @@ macro_rules! dt_test {
|
||||||
$destroy_method(dev);
|
$destroy_method(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_date_century() {
|
|
||||||
let d = new_date(2100, 8, 13);
|
|
||||||
let mut dev = $create_method(&$mac_trans_write!(
|
|
||||||
DOW,
|
|
||||||
[0b0000_0110, 0b0001_0011, 0b1000_1000, 0]
|
|
||||||
));
|
|
||||||
dev.set_date(&d).unwrap();
|
|
||||||
$destroy_method(dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get_time() {
|
fn get_time() {
|
||||||
let t = NaiveTime::from_hms_opt(23, 59, 58).unwrap();
|
let t = NaiveTime::from_hms_opt(23, 59, 58).unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue