Skip to content

Commit 6ced7ec

Browse files
committed
ECCX08 monotonic counter feature
- rename keyId to counterId - add counterId value check - split functions to keep library style
1 parent 0fe82c9 commit 6ced7ec

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/ECCX08.cpp

+38-12
Original file line numberDiff line numberDiff line change
@@ -548,51 +548,77 @@ int ECCX08Class::nonce(const byte data[])
548548
return challenge(data);
549549
}
550550

551-
long ECCX08Class::incrementCounter(int keyId)
551+
int ECCX08Class::incrementCounter(int counterId, long& counter)
552552
{
553-
uint32_t counter; // the counter can go up to 2,097,151
553+
if (counterId < 0 || counterId > 1) {
554+
return 0;
555+
}
554556

555557
if (!wakeup()) {
556-
return -1;
558+
return 0;
557559
}
558560

559-
if (!sendCommand(0x24, 1, keyId)) {
560-
return -1;
561+
if (!sendCommand(0x24, 1, counterId)) {
562+
return 0;
561563
}
562564

563565
delay(20);
564566

565567
if (!receiveResponse(&counter, sizeof(counter))) {
566-
return -1;
568+
return 0;
567569
}
568570

569571
delay(1);
570572
idle();
571573

574+
return 1;
575+
}
576+
577+
long ECCX08Class::incrementCounter(int counterId)
578+
{
579+
long counter; // the counter can go up to 2,097,151
580+
581+
if(!incrementCounter(counterId, counter)) {
582+
return -1;
583+
}
584+
572585
return counter;
573586
}
574587

575-
long ECCX08Class::readCounter(int keyId)
588+
int ECCX08Class::readCounter(int counterId, long& counter)
576589
{
577-
uint32_t counter; // the counter can go up to 2,097,151
590+
if (counterId < 0 || counterId > 1) {
591+
return 0;
592+
}
578593

579594
if (!wakeup()) {
580-
return -1;
595+
return 0;
581596
}
582597

583-
if (!sendCommand(0x24, 0, keyId)) {
584-
return -1;
598+
if (!sendCommand(0x24, 0, counterId)) {
599+
return 0;
585600
}
586601

587602
delay(20);
588603

589604
if (!receiveResponse(&counter, sizeof(counter))) {
590-
return -1;
605+
return 0;
591606
}
592607

593608
delay(1);
594609
idle();
595610

611+
return 1;
612+
}
613+
614+
long ECCX08Class::readCounter(int counterId)
615+
{
616+
long counter; // the counter can go up to 2,097,151
617+
618+
if(!readCounter(counterId, counter)) {
619+
return -1;
620+
}
621+
596622
return counter;
597623
}
598624

src/ECCX08.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ class ECCX08Class
6666

6767
int nonce(const byte data[]);
6868

69-
long incrementCounter(int keyId);
70-
long readCounter(int keyId);
69+
int incrementCounter(int counterId, long& counter);
70+
long incrementCounter(int counterId);
71+
int readCounter(int counterId, long& counter);
72+
long readCounter(int counterId);
7173

7274
private:
7375
int wakeup();

0 commit comments

Comments
 (0)