Added a new custom APDU (88h) for setting and retrieving datetime.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2022-04-04 15:48:04 +02:00
parent 13d17fc4f7
commit d49e7be972
2 changed files with 43 additions and 13 deletions

View File

@@ -1605,6 +1605,21 @@ void led_off_all()
#endif #endif
} }
void init_rtc() {
rtc_init();
datetime_t dt = {
.year = 2020,
.month = 1,
.day = 1,
.dotw = 3, // 0 is Sunday, so 5 is Friday
.hour = 00,
.min = 00,
.sec = 00
};
rtc_set_datetime(&dt);
}
extern void neug_task(); extern void neug_task();
pico_unique_board_id_t unique_id; pico_unique_board_id_t unique_id;
@@ -1641,7 +1656,7 @@ int main(void)
low_flash_init(); low_flash_init();
rtc_init(); init_rtc();
while (1) while (1)
{ {

View File

@@ -1717,18 +1717,33 @@ static int cmd_derive_asym() {
static int cmd_datetime() { static int cmd_datetime() {
if (P1(apdu) != 0x0 || P2(apdu) != 0x0) if (P1(apdu) != 0x0 || P2(apdu) != 0x0)
return SW_INCORRECT_P1P2(); return SW_INCORRECT_P1P2();
if (apdu.cmd_apdu_data_len != 8) if (apdu.cmd_apdu_data_len == 0) {
return SW_WRONG_LENGTH(); datetime_t dt;
datetime_t dt; if (!rtc_get_datetime(&dt))
dt.year = (apdu.cmd_apdu_data[0] << 8) | (apdu.cmd_apdu_data[1]); return SW_EXEC_ERROR();
dt.month = apdu.cmd_apdu_data[2]; res_APDU[res_APDU_size++] = dt.year >> 8;
dt.day = apdu.cmd_apdu_data[3]; res_APDU[res_APDU_size++] = dt.year & 0xff;
dt.dotw = apdu.cmd_apdu_data[4]; res_APDU[res_APDU_size++] = dt.month;
dt.hour = apdu.cmd_apdu_data[5]; res_APDU[res_APDU_size++] = dt.day;
dt.min = apdu.cmd_apdu_data[6]; res_APDU[res_APDU_size++] = dt.dotw;
dt.sec = apdu.cmd_apdu_data[7]; res_APDU[res_APDU_size++] = dt.hour;
if (!rtc_set_datetime(&dt)) res_APDU[res_APDU_size++] = dt.min;
return SW_WRONG_DATA(); res_APDU[res_APDU_size++] = dt.sec;
}
else {
if (apdu.cmd_apdu_data_len != 8)
return SW_WRONG_LENGTH();
datetime_t dt;
dt.year = (apdu.cmd_apdu_data[0] << 8) | (apdu.cmd_apdu_data[1]);
dt.month = apdu.cmd_apdu_data[2];
dt.day = apdu.cmd_apdu_data[3];
dt.dotw = apdu.cmd_apdu_data[4];
dt.hour = apdu.cmd_apdu_data[5];
dt.min = apdu.cmd_apdu_data[6];
dt.sec = apdu.cmd_apdu_data[7];
if (!rtc_set_datetime(&dt))
return SW_WRONG_DATA();
}
return SW_OK(); return SW_OK();
} }