-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenpaygo_token.cpp
116 lines (105 loc) · 3.65 KB
/
openpaygo_token.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "openpaygo_token.h"
// We need to include the C files manually for the compiler to work
extern "C" {
#include "openpaygo_token/opaygo_core.c"
#include "openpaygo_token/opaygo_decoder.c"
#include "openpaygo_token/opaygo_value_utils.c"
#include "openpaygo_token/restricted_digit_set_mode.c"
#include "openpaygo_token/siphash.c"
}
TokenData Output;
void setupToken() {
switchLoad(false);
}
void loopToken() {
if (isActive()) {
switchLoad(true);
} else {
switchLoad(false);
}
}
int tokenReceived(uint32_t receivedToken) {
debugPrint("INFO: Token received: ");
debugPrint(receivedToken);
Output = GetDataFromToken(receivedToken, &lastCount, &usedTokens, startingCode, secretKey);
debugPrint(" | Token Value: ");
debugPrint(Output.Value);
debugPrint(" | Token Count: ");
debugPrint(Output.Count);
debugPrint(" | Max Count: ");
debugPrintln(lastCount);
return updateDeviceStatusFromTokenValue(Output.Value, Output.Count);
}
bool isActive() {
if (!paygDisabled) {
return (activeUntil > getTimeInSeconds());
}
return true;
}
int updateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {
if (TokenValue == -1) {
invalidTokenCount++;
updateInvalidTokenWaitingPeriod();
blinkRedLED(5);
return TOKEN_INVALID;
} else if (TokenValue == -2) {
blinkGreenLED(1); // We blink the green LED once to show that the token
// was valid but isnt anymore
return TOKEN_ALREADY_USED;
} else {
invalidTokenCount = 0;
if (TokenValue == COUNTER_SYNC_VALUE) {
blinkGreenLED(3); // We blink green twice to show that the token is good
} else if (TokenValue == PAYG_DISABLE_VALUE) {
paygDisabled = true;
blinkGreenLED(5); // We blink green twice to show that the device
// is active forever
return TOKEN_PAYG_DISABLED;
} else {
if (ActivationCount % 2) {
paygDisabled = false;
setTime(TokenValue);
} else {
addTime(TokenValue);
}
blinkGreenLED(2); // We blink green twice to show that the token is good
}
saveActivationData(); // We store in every case
return getActivationTimeLeft();
}
}
uint32_t getActivationTimeLeft() {
return activeUntil - getTimeInSeconds();
}
void updateInvalidTokenWaitingPeriod() {
// We check that it does not become unbearably long
if (invalidTokenCount > 11) {
invalidTokenCount = 11;
}
// We add some forgiveness for the first 2 errors
if (invalidTokenCount > 2) {
tokenEntryLockedUntil = getTimeInSeconds() + pow(2, invalidTokenCount - 2) * 60;
}
}
bool tokenEntryAllowed() {
if (tokenEntryLockedUntil > getTimeInSeconds()) {
return false;
} else {
return true;
}
}
void addTime(int ActivationValue) {
uint32_t Now = getTimeInSeconds();
int NumberOfSecondsToActivate = (ActivationValue * 3600 * 24) / TIME_DIVIDER;
if (activeUntil < Now) {
activeUntil = Now;
}
activeUntil += NumberOfSecondsToActivate; // We add the number of days (converted in
// seconds for to compare to our RTC time)
}
void setTime(int ActivationValue) {
uint32_t Now = getTimeInSeconds();
int NumberOfSecondsToActivate = (ActivationValue * 3600 * 24) / TIME_DIVIDER;
activeUntil = Now + NumberOfSecondsToActivate; // We set the number of days (converted in
// seconds for to compare to our RTC time)
}