This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430F5529: Password Timeout

Part Number: MSP430F5529

Tool/software: Code Composer Studio

I have a program that asks for a user name and password for a simple login via terminal, and it works correctly.  I am curious what would be the best way to implement a timeout using the timers on this board to only allow ~30 seconds for password input.  Should I use an ISR in the password function?

//Instructions: Set the following parameters in PuTTY
//Port:         COM4
//Baud rate:    115200
//Data bits:    8
//Parity:       None
//Stop bits:    1
//Flow Control: None

#include <msp430.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

//values for outputs and input
char firstquestion[29] = "Please enter the user name: ";
const char username[10] = "user name";
const char username2[12] = "user name 2";
char usernameinput[12];
char secondquestion[21] = "Enter the password: ";
const char password[9] = "password";
const char password2[10] = "password2";
char passwordinput[10];
char correctanswer[22] = "Correct!!!";
char incorrectanswer[35] = "Incorrect user name or password!!!";


//loop counter
unsigned int i=0;

//new line and carriage return
void newline(void)
{
    while(!(UCA0IFG&UCTXIFG));  //waits for character
    UCA0TXBUF='\n';             //sets character to new line
    while(!(UCA0IFG&UCTXIFG));  //waits for character
    UCA0TXBUF='\r';             //sets character to carriage return
}
//prompt for user name
void userdata(void)
{
    for(i=0; i<strlen(firstquestion);i++)   //loops through first question elements and outputs
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=firstquestion[i];         //sets character to for element of first question
    }

    newline();                              //calls new line and carriage return function

    for(i=0; i<50;i++)                      //loops through received characters loop needs to go long enough for password and user name
    {
        while(!(UCA0IFG&UCRXIFG));          //waits for character
        if(UCA0RXBUF== 0x0D)                //if character is the "Enter key" will break from receiving characters
            break;
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=UCA0RXBUF;                //sets transmit buffer equal to receive buffer to echo print what user types
        usernameinput[i]=UCA0RXBUF;         //sets first element of user name equal to first character received from transmit buffer
    }
    UCA0RXBUF = 0;                          //clears transmit buffer
    usernameinput[i]= 0x00;                 //adds null character to end of user name array
}

void passdata(void)
{
    newline();
    for(i=0; i<strlen(secondquestion);i++)  //loops through second question elements and outputs
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=secondquestion[i];        //sets character to for element of second question
    }

    newline();                              //calls new line and carriage return function

    for(i=0; i<50;i++)                      //loops through received characters loop needs to go long enough for password and user name
    {
        while(!(UCA0IFG&UCRXIFG));          //waits for character
        if(UCA0RXBUF== 0x0D)                // if character is the "Enter key" will break from receiving characters
            break;
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF='*';                      //instead of echo printing prints '*' for the bonus
        UCA0TXBUF=UCA0RXBUF;
        passwordinput[i]=UCA0RXBUF;         //sets first element of password equal to first character received from transmit buffer
    }
    UCA0RXBUF = 0;                          //clears transmit buffer
    passwordinput[i]= 0x00;                 //adds null character to end of password array
}

void correct(void)
{
    newline();                              //calls new line and carriage return function

    for(i=0; i<strlen(correctanswer);i++)   //loops through the output if the user name and password are character
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=correctanswer[i];         //sets character to each element of output
    }

    newline();                              //calls new line and carriage return function
}

void incorrect(void)
{
   newline();                               //calls new line and carriage return function

    for(i=0; i<strlen(incorrectanswer);i++) //loops through output if user name and password are incorrect
    {
        while(!(UCA0IFG&UCTXIFG));          //waits for character
        UCA0TXBUF=incorrectanswer[i];       //sets character to each element of output
    }

    newline();                              //calls newline and carriage return function
}

void comparedata(void)
{
    unsigned int x=0;                       //values for testing user name
    unsigned int y=0;                       //value for testing password
    for(i=0; i<strlen(username);i++)        //loops through user name
    {
        if(username[i]==usernameinput[i])   //if user name element is equal to the inputed user name element
            x=1;                            //sets x to 1
        else if(username2[i]==usernameinput[i])
            x=1;
        else
        {
            x=0;                            //if not equal sets x to 0
            break;                          //also breaks if it is not equal
        }
    }
    for(i=0; i<strlen(password);i++)        //loops through password
    {
        if(password[i]==passwordinput[i])   //if password element is equal to the inputed password element
            y=1;                            //set y to 1
        else if(password2[i]==passwordinput[i])
            x=1;
        else
        {
            y=0;                            //if not equal sets y to 0
            break;                          //also breaks if it is not equal
        }
    }
    if((x && y) == 1)                       //if x and y equal 1 then both password and user name are the same
        correct();                          //if they are the same calls correct function to print specific output
    else
        incorrect();                        //if either of them are not equal call incorrect function to print specific output
}

void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;   // Stop WDT
    P3SEL |= BIT3 + BIT4;       // 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 |= UCBRS0;         // Modulation (UCBRS0=0x01, UCOS16=0)
    UCA0CTL1 &= ~UCSWRST;       // Clear software reset to initialize USCI state machine

    while(1)
    {
        userdata();                             //calls user name function
        passdata();                             //calls password function
        comparedata();                          //calls compare function
    }
}

  • I am also curious if it is possible to change the text colors on the fly, like outputting "Correct!!!" in green and "Incorrect user name or password!!!' in red, for example.

  • A simple timeout mechanism would be to use a timer driven by ACLK (TASSEL__ACLK, 32kHz) with MC__UP and CCR0=32768u. Just let it run all the time. In the A0 ISR, just decrement a ("volatile") counter variable, call it timeout_count. Stop decrementing at 0.. 

    To start the timeout, store a number of seconds in timeout_count and poll it along with RXIFG, something like: 

    >        while(!(UCA0IFG&UCRXIFG) && timeout_count > 0);          //waits for character

     

  • PuTTY can emulate a VT100 terminal, though I think you have to explicitly configure it to do so. The VT100 can be controlled using (in-line) Escape sequences generally of the form "ESC[<stuff>".  (Yes, that's a left-square-bracket.)

    The VT100s I used (40 years ago) were monochrome, but somewhere along the line someone evidently defined Escape sequences for text color:

    http://wiki.parsec.com/howto/add_color_to_your_terminal_text

    The only hazard comes if you send such a stream to a terminal emulator that doesn't understand it, in which case those sequences are pretty funny-looking. I vaguely recall an Escape sequence that allowed the program to ask "Are you a VT100?", but I don't recall how it worked.

**Attention** This is a public forum