Merge pull request #321 from resoli/master

Implementation of card_ctl function with SC_CARDCTL_GET_SERIALNR
This commit is contained in:
Frank Morgner 2014-12-19 11:47:26 +01:00
commit 4167455362
1 changed files with 60 additions and 0 deletions

View File

@ -479,6 +479,65 @@ static int itacns_select_file(sc_card_t *card,
SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_NORMAL, r);
}
static int itacns_get_serialnr(sc_card_t *card, sc_serial_number_t *serial)
{
sc_path_t path;
sc_file_t *file;
size_t len;
int r;
u8 rbuf[256];
if (!serial) return SC_ERROR_INVALID_ARGUMENTS;
/* see if we have cached serial number */
if (card->serialnr.len) {
memcpy(serial, &card->serialnr, sizeof(*serial));
return SC_SUCCESS;
}
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Reading EF_IDCarta.\n");
sc_format_path("3F0010001003", &path);
r = sc_select_file(card, &path, &file);
if (r != SC_SUCCESS) {
return SC_ERROR_WRONG_CARD;
}
len = file->size;
//Returned file->size should be 16.
//We choose to not consider it as critical, because some cards
//do not return FCI/FCP templates that include the file size.
//Notify abnormal lenght anyway.
if (len != 16) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL,
"Unexpected file length of EF_IDCarta (%lu)\n",
(unsigned long) len);
}
r = sc_read_binary(card, 0, rbuf, 256, 0);
if ( r != 16 ) {
return SC_ERROR_WRONG_CARD;
}
/* cache serial number */
memcpy(card->serialnr.value, rbuf, 16);
card->serialnr.len = 16;
/* copy and return serial number */
memcpy(serial, &card->serialnr, sizeof(*serial));
return SC_SUCCESS;
}
static int
itacns_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr)
{
switch (cmd) {
case SC_CARDCTL_GET_SERIALNR:
return itacns_get_serialnr(card, ptr);
}
return SC_ERROR_NOT_SUPPORTED;
}
static struct sc_card_driver * sc_get_driver(void)
{
@ -494,6 +553,7 @@ static struct sc_card_driver * sc_get_driver(void)
itacns_ops.read_binary = itacns_read_binary;
itacns_ops.list_files = itacns_list_files;
itacns_ops.select_file = itacns_select_file;
itacns_ops.card_ctl = itacns_card_ctl;
return &itacns_drv;
}