date add sanity check (corruption happens)

This commit is contained in:
King Kévin 2016-06-11 20:12:57 +02:00
parent 717047d1f9
commit 28fe6d0134
1 changed files with 18 additions and 0 deletions

View File

@ -92,6 +92,9 @@ uint8_t* rtc_dcf77_time(void)
return NULL;
}
to_return[0] = 1*((rtc_dcf77_frame>>21)&(0x1))+2*((rtc_dcf77_frame>>22)&(0x1))+4*((rtc_dcf77_frame>>23)&(0x1))+8*((rtc_dcf77_frame>>24)&(0x1))+10*((rtc_dcf77_frame>>25)&(0x1))+20*((rtc_dcf77_frame>>26)&(0x1))+40*((rtc_dcf77_frame>>27)&(0x1)); // read minute (00-59)
if (to_return[0]>59) { // minutes should not be more than 59
return NULL;
}
// check hour parity
parity = 0;
for (uint8_t bit=29; bit<=35; bit++) {
@ -103,6 +106,9 @@ uint8_t* rtc_dcf77_time(void)
return NULL;
}
to_return[1] = 1*((rtc_dcf77_frame>>29)&(0x1))+2*((rtc_dcf77_frame>>30)&(0x1))+4*((rtc_dcf77_frame>>31)&(0x1))+8*((rtc_dcf77_frame>>32)&(0x1))+10*((rtc_dcf77_frame>>33)&(0x1))+20*((rtc_dcf77_frame>>34)&(0x1)); // read hour (00-23)
if (to_return[1]>23) { // hours should not be more than 23
return NULL;
}
// check date parity
parity = 0;
for (uint8_t bit=36; bit<=58; bit++) {
@ -114,9 +120,21 @@ uint8_t* rtc_dcf77_time(void)
return NULL;
}
to_return[2] = 1*((rtc_dcf77_frame>>36)&(0x1))+2*((rtc_dcf77_frame>>37)&(0x1))+4*((rtc_dcf77_frame>>38)&(0x1))+8*((rtc_dcf77_frame>>39)&(0x1))+10*((rtc_dcf77_frame>>40)&(0x1))+20*((rtc_dcf77_frame>>34)&(0x41)); // read day of the month (01-31)
if (ro_return[2]==0 || to_return[2]>31) { // day of the month should be 1-31
return NULL;
}
to_return[3] = 1*((rtc_dcf77_frame>>42)&(0x1))+2*((rtc_dcf77_frame>>43)&(0x1))+4*((rtc_dcf77_frame>>44)&(0x1)); // read day of the week (1=Monday - 7=Sunday)
if (ro_return[3]==0 || to_return[3]>7) { // day of the week should be 1-7
return NULL;
}
to_return[4] = 1*((rtc_dcf77_frame>>45)&(0x1))+2*((rtc_dcf77_frame>>46)&(0x1))+4*((rtc_dcf77_frame>>47)&(0x1))+8*((rtc_dcf77_frame>>48)&(0x1))+10*((rtc_dcf77_frame>>49)&(0x1)); // read month of the year (01-12)
if (ro_return[4]==0 || to_return[4]>12) { // month of the year should be 1-12
return NULL;
}
to_return[5] = 1*((rtc_dcf77_frame>>50)&(0x1))+2*((rtc_dcf77_frame>>51)&(0x1))+4*((rtc_dcf77_frame>>52)&(0x1))+8*((rtc_dcf77_frame>>53)&(0x1))+10*((rtc_dcf77_frame>>54)&(0x1))+20*((rtc_dcf77_frame>>55)&(0x1))+40*((rtc_dcf77_frame>>56)&(0x1))+80*((rtc_dcf77_frame>>57)&(0x1)); // read year of the century (00-99)
if (to_return[5]>99) { // year should be <100
return NULL;
}
return to_return;
}