Skip to content

Commit

Permalink
Only change speed after ATR if TA2 bit 5 is unset
Browse files Browse the repository at this point in the history
Based on ISO 7816-3 8.3:

If bit 5 is set to 0, then the integers Fi and Di defined above by TA1 shall apply.
If bit 5 is set to 1, then implicit values (not defined by the interface bytes) shall apply.
  • Loading branch information
yarrick committed May 10, 2023
1 parent 4e0f704 commit 9363dcd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 7 additions & 3 deletions atr.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void atr_init(struct atr *atr) {
atr->bytes_left = 2;
atr->first_protocol_suggested = NO_VALUE;
atr->ta1_value = NO_VALUE;
atr->ta2_value = NO_VALUE;
}

static int handle_t_bits(struct atr *atr, unsigned char data, unsigned *complete) {
Expand Down Expand Up @@ -44,7 +45,7 @@ static void handle_ta_byte(struct atr *atr, unsigned char data) {
atr->ta1_value = data;
break;
case 2:
atr->ta2_seen = 1;
atr->ta2_value = data;
break;
}
}
Expand Down Expand Up @@ -100,7 +101,10 @@ void atr_result(struct atr *atr, unsigned *new_proto, unsigned *new_speed) {
if (new_proto && atr->first_protocol_suggested != NO_VALUE) {
*new_proto = atr->first_protocol_suggested;
}
if (new_speed && atr->ta1_value != NO_VALUE && atr->ta2_seen) {
*new_speed = atr->ta1_value;
if (new_speed && atr->ta1_value != NO_VALUE) {
if (atr->ta2_value != NO_VALUE && (atr->ta2_value & 0x10) == 0) {
// Switch if TA2 bit 5 is cleared.
*new_speed = atr->ta1_value;
}
}
}
2 changes: 1 addition & 1 deletion atr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct atr {
unsigned num_historical_bytes;
unsigned t_cycle;
unsigned ta1_value;
unsigned ta2_seen;
unsigned ta2_value;
};

void atr_init(struct atr *atr);
Expand Down
6 changes: 5 additions & 1 deletion atr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ static const struct sample {
.data = {0x3B, 0x93, 0x95, 0x80, 0x1F, 0xC7, 0x80, 0x31, 0x80, 0x6F},
.len = 10,
},
{ // TA(1) and TA(2) supplied, new speed in use.
{ // TA(1) with speed, TA(2) with bit 5 cleared, new speed in use.
.data = {0x3B, 0xD2, 0x13, 0xFF, 0x10, 0x80, 0x07, 0x14},
.len = 8, .speed = 0x13,
},
{ // TA(1) with speed, TA(2) with bit 5 set, no speed change.
.data = {0x3B, 0xD2, 0x13, 0xFF, 0x10, 0x90, 0x07, 0x14},
.len = 8,
},
{ // TD(1) present but as last byte.
.data = {0x3B, 0x80, 0x00}, .len = 3,
},
Expand Down

0 comments on commit 9363dcd

Please sign in to comment.