Skip to content

Commit 71d2f67

Browse files
* Terminal line now clears when invalid character is entered.
* Terminal shows the user entering valid lock sequence. * Refactored for clarity.
1 parent 950f028 commit 71d2f67

File tree

1 file changed

+20
-13
lines changed
  • secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X

1 file changed

+20
-13
lines changed

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/main.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <stdio.h>
2323
#include <string.h>
24+
#include <stdbool.h>
2425

2526
#define WINDOW_SIZE 10
2627
#define UNLOCK_COMMAND "LOCKDEVICE"
@@ -31,6 +32,7 @@ static void ClearTerminalLine(void);
3132
static void MoveCursor(int row);
3233
static void HideCursor(void);
3334
static void PrintWarning(void);
35+
static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex);
3436
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex);
3537
static void ResetWindowOnMismatch(char *window, int *windowIndex);
3638
static void CheckForUnlockCommand(char *window, int *windowIndex);
@@ -43,13 +45,13 @@ int main(void)
4345
SYSTEM_Initialize();
4446
ClearTerminalScreen();
4547
PrintWarning();
46-
4748
while (1)
4849
{
4950
if (UART1_IsRxReady())
5051
{
5152
char receivedChar = UART1_Read();
5253
ProcessReceivedChar(receivedChar, window, &windowIndex);
54+
CheckForUnlockCommand(window, &windowIndex);
5355
}
5456
}
5557
}
@@ -84,24 +86,30 @@ static void PrintWarning(void)
8486

8587
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex)
8688
{
87-
MoveCursor(10);
89+
bool isCharValid = (*windowIndex < strlen(UNLOCK_COMMAND)) && (receivedChar == UNLOCK_COMMAND[*windowIndex]);
8890

89-
if (*windowIndex < WINDOW_SIZE && receivedChar == UNLOCK_COMMAND[*windowIndex])
91+
if (isCharValid)
9092
{
91-
printf("%c", receivedChar);
92-
window[(*windowIndex)++] = receivedChar;
93-
window[*windowIndex] = '\0';
94-
CheckForUnlockCommand(window, windowIndex);
93+
AppendCharToWindow(receivedChar, window, windowIndex);
94+
MoveCursor(10);
95+
printf("%s", window);
9596
}
9697
else
9798
{
98-
ClearTerminalLine();
9999
ResetWindowOnMismatch(window, windowIndex);
100100
}
101101
}
102102

103+
static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex)
104+
{
105+
window[(*windowIndex)++] = receivedChar;
106+
window[*windowIndex] = '\0';
107+
}
108+
103109
static void ResetWindowOnMismatch(char *window, int *windowIndex)
104110
{
111+
MoveCursor(10);
112+
ClearTerminalLine();
105113
MoveCursor(5);
106114
ClearTerminalLine();
107115
printf("Invalid character entered. Try again.");
@@ -111,14 +119,13 @@ static void ResetWindowOnMismatch(char *window, int *windowIndex)
111119

112120
static void CheckForUnlockCommand(char *window, int *windowIndex)
113121
{
114-
int patternLength = strlen(UNLOCK_COMMAND);
115-
116-
if (*windowIndex == patternLength)
122+
if (strncmp(window, UNLOCK_COMMAND, *windowIndex) == 0 && *windowIndex == strlen(UNLOCK_COMMAND))
117123
{
118124
MoveCursor(5);
119125
ClearTerminalLine();
120-
printf("Unlock command for ICSP Inhibit received.\n");
121-
HideCursor();
126+
printf("Unlock command successful.\n");
127+
MoveCursor(10);
128+
ClearTerminalLine();
122129
*windowIndex = 0;
123130
memset(window, 0, WINDOW_SIZE + 1);
124131
}

0 commit comments

Comments
 (0)