-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
160 lines (136 loc) · 4.1 KB
/
main.c
File metadata and controls
160 lines (136 loc) · 4.1 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "KeyboardHID.h"
#include "german_keyboardCodes.h"
#include "Handlers.h"
#include "ReportUtils.h"
#include "IndexToChar.h"
#include "CharCodeGerman.h"
#define LED1 P1_1
#define LED2 P1_0
#define TICKS_PER_SECOND 10000
volatile uint32_t system_ticks;
uint32_t characterResponseTimes[84];
char discoveredPasswordBuffer[20];
uint8_t nameCharPosition = 0;
uint8_t awaitingKeyRelease = 0;
uint8_t testingCharacterIndex = 0;
static uint8_t detectedCharacterIndex = 0;
uint8_t passwordOutputPosition = 0;
uint8_t extractedPasswordLength = 0;
bool nameKeyReleased = true;
bool capsLockPhaseFinished = false;
bool shouldSendEnterKey = false;
bool readyForNextCharacter = false;
static bool passwordExtractionComplete = false;
const char nameString[] = "echo \"mehmet arslan\" > $HOME/03811532";
XMC_SCU_CLOCK_CONFIG_t clock_config =
{
.syspll_config.p_div = 2,
.syspll_config.n_div = 80,
.syspll_config.k_div = 4,
.syspll_config.mode = XMC_SCU_CLOCK_SYSPLL_MODE_NORMAL,
.syspll_config.clksrc = XMC_SCU_CLOCK_SYSPLLCLKSRC_OSCHP,
.enable_oschp = true,
.calibration_mode = XMC_SCU_CLOCK_FOFI_CALIBRATION_MODE_FACTORY,
.fsys_clksrc = XMC_SCU_CLOCK_SYSCLKSRC_PLL,
.fsys_clkdiv = 1,
.fcpu_clkdiv = 1,
.fccu_clkdiv = 1,
.fperipheral_clkdiv = 1
};
void SystemCoreClockSetup(void);
void SysTick_Handler(void)
{
system_ticks++;
}
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize);
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, const uint8_t ReportID, const uint8_t ReportType, const void* ReportData, const uint16_t ReportSize);
uint8_t findTheChar();
int main(void)
{
XMC_GPIO_SetMode(LED1, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
XMC_GPIO_SetMode(LED2, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
USB_Init();
for (int i = 0; i < 800000; i++)
;
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
XMC_GPIO_SetOutputHigh(LED2);
while (1)
{
HID_Device_USBTask(&Keyboard_HID_Interface);
}
}
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize)
{
USB_KeyboardReport_Data_t* report = (USB_KeyboardReport_Data_t*)ReportData;
*ReportSize = sizeof(USB_KeyboardReport_Data_t);
static bool isReleased = true;
static bool capsLockPressed = false;
if (!passwordExtractionComplete)
{
HandlePasswordInput(report, &isReleased);
}
else if (!capsLockPhaseFinished)
{
HandleCapsLockToggle(report, &capsLockPressed);
}
else if (capsLockPhaseFinished)
{
HandleNameOutput(report);
}
return true;
}
uint8_t findTheChar()
{
uint8_t longest_time_id = 0;
uint32_t longest_time = 0;
for (int i = 0; i < 84; i++)
{
if (longest_time < characterResponseTimes[i])
{
longest_time = characterResponseTimes[i];
longest_time_id = i;
}
}
return longest_time_id - 1;
}
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, const uint8_t ReportID, const uint8_t ReportType, const void* ReportData, const uint16_t ReportSize)
{
uint8_t* report = (uint8_t *)ReportData;
if (*report & HID_KEYBOARD_LED_NUMLOCK)
{
XMC_GPIO_SetOutputHigh(LED1);
if (testingCharacterIndex == 84)
{
detectedCharacterIndex = findTheChar();
discoveredPasswordBuffer[extractedPasswordLength++] = detectedCharacterIndex;
testingCharacterIndex = 0;
}
readyForNextCharacter = true;
}
else
{
XMC_GPIO_SetOutputLow(LED1);
readyForNextCharacter = false;
}
if (*report & HID_KEYBOARD_LED_CAPSLOCK)
{
XMC_GPIO_SetOutputHigh(LED2);
passwordExtractionComplete = true;
}
else
{
XMC_GPIO_SetOutputLow(LED2);
}
}
// This function is given by the instructor
void SystemCoreClockSetup(void)
{
/* Setup settings for USB clock */
XMC_SCU_CLOCK_Init(&clock_config);
XMC_SCU_CLOCK_EnableUsbPll();
XMC_SCU_CLOCK_StartUsbPll(2, 64);
XMC_SCU_CLOCK_SetUsbClockDivider(4);
XMC_SCU_CLOCK_SetUsbClockSource(XMC_SCU_CLOCK_USBCLKSRC_USBPLL);
XMC_SCU_CLOCK_EnableClock(XMC_SCU_CLOCK_USB);
SystemCoreClockUpdate();
}