1
0
UAHCode/CPE325/FinalProject/FinalProject.c

588 lines
17 KiB
C
Raw Permalink Normal View History

2022-08-28 21:12:16 +00:00
#include <msp430.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SW1_PRESSED ((BIT0&P1IFG)==0) // SW1 Status
#define SW2_PRESSED ((BIT1&P1IFG)==0) // SW2 Status
#define checkTime() (clockCheck=true)
#define GREENLED() (P2OUT|=BIT2)
#define REDLED() (P5OUT|=BIT1)
#define GREENLEDOFF() (P2OUT&=~BIT2)
#define REDLEDOFF() (P5OUT&=~BIT1)
// Struct used to define users
struct user
{
char *name;
char *password;
int admin;
};
struct user andrew;
struct user joe;
struct user bill;
struct user jeff;
struct user andrew = {.name = "Andrew", .admin = 1, .password = "an"};
struct user joe = { .name = "Joe", .admin = 1, .password = "jo" };
struct user bill = { .name = "Bill", .admin = 0, .password = "bi" };
struct user jeff = { .name = "Jeff", .admin = 0, .password = "je" };
char user[];
char cpu[];
char askReset[];
char password[];
char command[];
//int admin;
int timeout=0;
int timer=0;
int Up=1;
bool clockCheck = false;
char SystemTime[];
//int diskspace = rand();
char time[];
bool test = false;
int update=0, timeCheck=0, upgrade=0, clock=0, timerCheck=0, data=0, logout=0;
bool admin = false;
bool nonadmin = false;
// Characters for clock
//#define RTC_STATE_SECONDS 0
//#define RTC_STATE_MINUTES 1
//#define RTC_STATE_HOURS 2
//
//volatile uint8_t timerCount = 0;
//volatile uint8_t rtcState[3] = {0, 0, 0};
unsigned int s, m, h;
int timerCount =0;
int diskspace;
// user tries to update and gets a buzzer and light
void clockFunc();
//upgrade the packages
void upgradeFunc();
// set a timer
void setTimer();
//void buzzer();
void resetBuzzer();
void setBuzzer();
void timerAInit(void){
// Enable Timer Interrupts
TACCTL0 = CCIE;
// SMCLK, Count to CCR0 Value, Divide by 8
TA0CTL = TASSEL_2 + MC_1 + ID_3;
// 1,000,000 / 8 / 25 = 5000
TACCR0 = 5000;
}
// checks string to see if correct,
// returns true if one of the users, false if not
bool stringCheck(char* string);
void updateFunc(); //update code when "update" is entered
void setup();
void promptOutput(); // Outputs prompt
void commandCheck(char* command);
void UART_setup(); // Configures UCI to work in the UART mode
// at the baud rate of 115200.
void UART_Send_Character(char my_char); // Sends a character via UART
void UARTsendString(char* string); // Sends a string via UART using
// send_Character(char c)
void UARTgetWord(char* buffer, int limit);
/* Receives characters via UART until it finds
the new line character or until the limit of
characters is exceeded. Writes that string
(excluding the new line character) to the
buffer allocated outside of the function.
Terminates the string with the null
character */
int stringLength(char *str); // String length
int main(void)
{
// WDTCTL = WDT_ADLY_1000; // 1 s interval
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
UART_setup();
setup();
//timerAInit();
UCA0TXBUF = 0x0D; // Carriage Return
clockFunc();
while(1){
//timeout =0;
UCA0TXBUF = 0x0D; // Carriage Return
UCA0TXBUF = 0x0D; // Carriage Return
clockFunc();
UARTsendString("\nPlease enter a username: "); // Output
UCA0RXBUF = 0x0D; // Carriage Return
clockFunc();
UARTgetWord(user, 30);
//clockFunc();
bool usercheck = stringCheck(user);
clockFunc();
UCA0TXBUF = 0x0D; // Carriage Return
clockFunc();
UARTsendString("\nPlease enter the password: ");
clockFunc();
//timeout = 1;
UARTgetWord(password, 30);
clockFunc();
bool passcheck = stringCheck(password);
clockFunc();
UCA0TXBUF = 0x0D; // Carriage Return
clockFunc();
if(usercheck && passcheck && admin) // If user and password match
{
clockFunc();
while(1){
clockFunc();
UCA0TXBUF = 0x0D; // Carriage Return
promptOutput();
clockFunc();
// char* commandEq;
UARTgetWord(command, 50);
//commandCheck(command);
//if(update==1)
if(strcmp(command, "update")==0)
{
GREENLED();
__delay_cycles(500000); // second
GREENLEDOFF();
clockFunc();
updateFunc();
//for(i=0; i ==stringLength(command); i++) command[i]=0x00;
}
else if(strcmp(command, "upgrade")==0)
{
GREENLED();
__delay_cycles(500000); // second
GREENLEDOFF();
clockFunc();
upgradeFunc();
}
else if(strcmp(command, "date")==0)
{
GREENLED();
__delay_cycles(500000); // second
GREENLEDOFF();
clockFunc();
clockFunc();
UARTsendString("The date is 1980, November 1. The time is ");
UARTsendString(SystemTime);
}
else if(strcmp(command, "timer")==0) // timer to reboot
{
GREENLED();
__delay_cycles(500000); // half-second
GREENLEDOFF();
clockFunc();
setTimer();
}
else if(strcmp(command, "data")==0)
{
GREENLED();
__delay_cycles(500000); // half-second
GREENLEDOFF();
clockFunc();
clockFunc();
setTimer();
}
else if(strcmp(command, "logout")==0)
{
GREENLED();
__delay_cycles(500000); // half-second
GREENLEDOFF();
clockFunc();
clockFunc();
break;
}
else
{
clockFunc();
REDLED();
setBuzzer();
__delay_cycles(500000); // half-second
resetBuzzer();
REDLEDOFF();
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("Unknown command.");
}
}
}
else if(usercheck && passcheck && admin==false)
{
while(1){
clockFunc();
UCA0TXBUF = 0x0D; // Carriage Return
promptOutput();
UARTgetWord(command, 50);
commandCheck(command);
if(strcmp(command, "upgrade")==0 || strcmp(command, "update")==0)
{
clockFunc();
REDLED();
setBuzzer();
__delay_cycles(500000); // half-second
resetBuzzer();
REDLEDOFF();
clockFunc();
}
else if(strcmp(command, "date")==0)
{
clockFunc();
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("The date is 1980, November 1. The time is ");
UARTsendString(SystemTime);
clockFunc();
}
else if(strcmp(command, "timer")==0)
{
GREENLED();
__delay_cycles(500000); // half-second
GREENLEDOFF();
clockFunc();
clockFunc();
setTimer();
}
else if(strcmp(command, "logout")==0)
{
GREENLED();
__delay_cycles(500000); // half-second
GREENLEDOFF();
clockFunc();
clockFunc();
break;
}
else
{
REDLED();
setBuzzer();
__delay_cycles(500000); // half-second
resetBuzzer();
REDLEDOFF();
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("Unknown command.");
}
}
}
else {
clockFunc();
UCA0TXBUF = '\n'; // Newline
UCA0TXBUF = 0x0D; // Carriage Return
REDLED();
setBuzzer();
__delay_cycles(500000); // half-second
resetBuzzer();
REDLEDOFF();
UARTsendString("\nInvalid username or password! Please try again.");
}
}
}
void promptOutput()
{
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nWelcome!! Enter a command: \n");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n1. Set timer: timer");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n2. Check for system updates: update");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n3. Update system: upgrade");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n4. Check system time: date");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n5. Logout: logout");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nCommand: ");
}
void UART_setup()
{
P2SEL |= BIT4 + BIT5; // Set USCI_A0 RXD/TXD to receive/transmit data
UCA0CTL1 |= UCSWRST; // Set software reset during initialization
UCA0CTL0 = 0; // USCI_A0 control register
UCA0CTL1 |= UCSSEL_2; // Clock source SMCLK
UCA0BR0 = 0x09; // 1048576 Hz / 115200 lower byte
UCA0BR1 = 0x00; // upper byte
UCA0MCTL = 0x02; // Modulation (UCBRS0=0x01, UCOS16=0)
UCA0CTL1 &= ~UCSWRST; // Clear software reset to initialize USCI state machine
}
void setBuzzer()
{
TBCCR0 = 250;
TBCCR4 = 125;
}
void resetBuzzer()
{
TBCCR0 = 3500;
TBCCR4 = 3600;
}
void UART_Send_Character(char my_char)
{
while (!(IFG2 & UCA0TXIFG)); // Wait for previous character to transmit
UCA0TXBUF = my_char; // Put character into tx buffer
}
void UARTsendString(char* string)
{
int i=0;
int len = stringLength(string);
for(i =0; i<=len; i++) //loops over word
{
UART_Send_Character(string[i]);
}
}
void UARTgetWord(char* buffer, int limit)
{
int i;
for(i =0; i < limit; i++){
while(!(IFG2&UCA0RXIFG)); // Wait for a new character
buffer[i] = UCA0RXBUF;
if(buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\0'){
buffer[i] = '\0'; // end is here in UCA0RXBUF
return;
}
while(!(IFG2&UCA0TXIFG)); // Wait until TXBUF is free
UCA0TXBUF = UCA0RXBUF; // TXBUF <= RXBUF (echo)
}
}
int stringLength(char *str) // gets string length for word
{
int i=0;
while(str[i] != '\0') i++;
return i;
}
// Port 1 interrupt service routine
#pragma vector = PORT1_VECTOR
__interrupt void Port1_ISR (void) {
if(SW1_PRESSED)
{
main();
}
P1IES |= BIT0; // P1.0 hi/low edge
P1IFG &= ~BIT0; // P1.0 IFG cleared
P1IES |= BIT1; // P1.1 hi/low edge
P1IFG &= ~BIT1; // P1.1 IFG cleared
}
bool stringCheck(char* string)
{
if(strcmp(string,andrew.name) == 0
|| strcmp(string,joe.name) == 0
|| strcmp(string,andrew.password)== 0
|| strcmp(string,joe.password)== 0)
{
admin = true;
return true;
}
else if(strcmp(string,bill.name) == 0
|| strcmp(string,jeff.password)== 0
|| strcmp(string,jeff.name) == 0
|| strcmp(string,bill.password)== 0)
{
admin = false;
return true;
}
else
{
// UARTsendString("False ");
return false;
}
}
void setTimer()
{
UARTsendString("The system will reboot in 30 seconds.");
//UARTgetWord(time,5);
WDTCTL = WDT_ADLY_1000;
timerCheck=1;
}
void commandCheck(char* command)
{
if(strcmp(command, "update")==0)
{
update = 1;
return;
}
else if(strcmp(command, "upgrade")==0)
{
upgrade = 1;
return;
}
else if(strcmp(command, "time")==0)
{
timerCheck = 1;
return;
}
else if(strcmp(command, "clock")==0)
{
clock = 1;
return;
}
else if(strcmp(command, "logout")==0)
{
logout = 1;
return;
}
}
// Timer Interrupt
//#pragma vector = TIMER0_A0_VECTOR
//__interrupt void Timer_A(void){
// // static int timerCount =0;
// static int m =0;
// static int h =0;
// static int s =0;
// s++;
// if (s >= 60){
// s = 0;
// m++;
// if (m >= 60){
// m = 0;
// h++;
// if (h >= 24) {
// h = 0;
// }
// }
// }
// sprintf(cpu, "%d", s);
// sprintf(SystemTime, "%d%d%d",h,m,s);
// //TA0CCTL0 &=~ TAIFG;
//}
void setup()
{
_EINT(); // Enable interrupts
IE1 |= WDTIE; // Enable WDT interrupt
P2DIR |= (BIT1 + BIT2); // P2.1 and P2.2 set up as output
P5DIR |= BIT1; // REDLED set up as output
P2OUT &= ~BIT1; // Turn on LED2
P2OUT &= ~BIT2; // Turn Off LED1
P1IE |= BIT0; // P1.0 interrupt enabled
P1IE |= BIT1; // P1.1 interrupt enabled
P1IES |= BIT0; // P1.0 hi/low edge
P1IFG &= ~BIT0; // P1.0 IFG cleared
P1IES |= BIT1; // P1.1 hi/low edge
P1IFG &= ~BIT1; // P1.1 IFG cleared
P2OUT &= ~(BIT1+BIT2); // Turn Off LED1
GREENLEDOFF();
REDLEDOFF();
TB0CTL = TBSSEL_2 + MC_1; // SMCLK Clock source Upmode
TB0CCR0 = 32765; // Set TB4 count value
TB0CCR4 = 32767; // Set TB4 count value
P3SEL |= 0x20; // P3.5 as special function
P3DIR |= 0x20; // P3.5 as digital output
TB0CCTL4 = OUTMOD_3; // Set/Rest mode
}
void updateFunc()
{
UCA0TXBUF = 0x0D; // Carriage Return
// P2OUT |= BIT2;
UARTsendString("\nUpdating packages.");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nHit:1 https://brave-browser-apt-release.s3.brave.com trusty InRelease");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nHit:2 https://deb.debian.org/debian bullseye InRelease");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nHit:3 https://repo.steampowered.com/steam stable InRelease");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nHit:4 https://typora.io/linux ./ InRelease [793 B]");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nReading package lists... Done");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n9 packages can be upgraded. Run 'apt list --upgradable' to see them.");
UCA0TXBUF = 0x0D; // Carriage Return
}
void clockFunc()
{
static int timerCount =0;
static int h=0,m=0,s=0;
timerCount++;
timerCount = 0;
s++;
if (s >= 60){
s = 0;
m++;
if (m >= 60){
m = 0;
h++;
if (h >= 24) {
h = 0;
}
}
}
sprintf(cpu, "%d", s);
sprintf(SystemTime, "%d%d%d",h,m,s);
}
void upgradeFunc()
{
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\nThe following packages will be upgraded: ");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n brave-browser steam:i386 steam-launcher steam-libs-amd64 steam-libs-i386:i386 typora");
UCA0TXBUF = 0x0D; // Carriage Return
UARTsendString("\n Preparing to unpack .../0-brave-browser_1.32.113_amd64.deb ...");
}
// Watchdog Timer interrupt service routine
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{
static int i =0;
//static int c =0;
i++;
if(i==30 && timerCheck==1){
setBuzzer();
UCA0TXBUF = 0x0D; // Carriage Return
timerCheck=0;
IFG2=1;
resetBuzzer();
// systemTime();
main();
}
// systemTime();
//IFG2=1;
}