CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Comparing two strings

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Indira Learner



Joined: 08 Sep 2017
Posts: 3

View user's profile Send private message

Comparing two strings
PostPosted: Fri Sep 08, 2017 8:16 am     Reply with quote

I need to get a string from the user and then compare it with another one, already found in the pic. I wrote a code for it, however, i want to change it so the code works immediately i press the send button and not wait until the enter key is pressed. Here is what I have done already:
Code:

#include <main.h>
#include <stdio.h>
#include <string.h>
#fuses HS
#use rs232(uart1, baud=9600)
char password[] = {"aaa"};

char input_str[];
char string_pass[];

#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_EXT

void  EXT_isr(void)
{
   reset_cpu();
}

#int_RDA

void  RDA_isr(void)
{
   int t;
   buffer[next_in] = getc();
   t = next_in;
   next_in =(next_in + 1) % BUFFER_SIZE;

   if(next_in == next_out)
      next_in = t;
}

#define bkbhit (next_in!=next_out)
BYTE bgetc() //Gets characters
{
   BYTE c;
   while(!bkbhit);
   c = buffer[next_out];
   next_out =(next_out + 1) % BUFFER_SIZE;
   return(c);
}

void get_string(char* s) //Stores the characters into a string
{
   unsigned int8 length;
   char c;
   
   length = 0;
   do
   {
      c = bgetc();
      if((c >= ' ')&&(c <= '~'))
         {
            s[length++] = c;
            putc(c);
         }
   }
   while(c != 13);// Check for enter key. How can I modify this part?
   s[length] = 0;
}

void toggle_function() //Toggle LED state if keywords match
{
   int i = 0;
   while(i < 20)
   {
      output_high(led1);
      delay_ms(150);
      i++;
   }
}

void compare_function(char *string) //Checks whether both keywords match
{
   if(strcmp(string,password) == 0)//Compares both strings
   {
      printf("\n\nCorrect Keyword ! ");
      toggle_function();
      break;
   }
   else
   {
      printf("\n\nERROR\n");
      output_low(led2);
      break;
   }
}

void main()
{
   output_D(0x00);
   
   clear_interrupt(int_EXT);
   enable_interrupts(int_EXT);
   
   clear_interrupt(int_RDA);
   enable_interrupts(int_RDA);
   enable_interrupts(GLOBAL);
 
   while(TRUE){
      printf("Type string here: ");
      get_string(input_str); //Calls the get_string function
     
      strcpy(string_pass,input_str); //Gets string pointer used int the compare function
      compare_function(string_pass); //Calls the compare functionhile(TRUE)
   }
}
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Fri Sep 08, 2017 8:36 am     Reply with quote

In the grand scheme of things, changing a password test from "upon enter" to "on the fly" is a security risk. That said, to do that sort of thing will require you to examine each character individually. A finite state machine (FSM) implementation would work very well for this.

For example:

Code:
#define INITIAL_STATE_LOCKED 0
#define PASSWORD_BEING_SENT_AND_ALL_GOOD 1
#define PASSWORD_BEING_SENT_AND_ITS_WRONG 2
#define UNLOCKED 3

unsigned int8 lock_state_FSM = INITIAL_STATE_LOCKED;
unsigned int8 num_received_characters = 0;
int1 lock_state_changed_flag = FALSE;
int1 lock_is_locked_flag = TRUE;


In your RDA interrupt you check the received character against:

Code:
password[num_received_characters++]


Match, and advance the lock_state_FSM to PASSWORD_BEING_SENT_AND_ALL_GOOD; mismatch and set it to PASSWORD_BEING_SENT_AND_ITS_WRONG. If you match all characters in the password, it's now UNLOCKED, and use the other flags to a) change the lock state, and b) alert the user that the lock has now changed state.
Indira Learner



Joined: 08 Sep 2017
Posts: 3

View user's profile Send private message

PostPosted: Sat Sep 09, 2017 3:43 am     Reply with quote

newguy wrote:

In the grand scheme of things, changing a password test from "upon enter" to "on the fly" is a security risk. That said, to do that sort of thing will require you to examine each character individually. A finite state machine (FSM) implementation would work very well for this.


Sorry, i'm still a newbie in programming so i'm not really sure what to do. So i don't know how to implement a final state machine in c. Do you have an example i could use?
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Sep 09, 2017 5:34 am     Reply with quote

You can make your code a lot simpler and smaller by NOT using 'strings'.

consider...

char password[] = {"abcdef"};

which is a 6 byte array.

Now in you 'valid_password_decoder' function simply
compare the 1st incoming character to the contents password[0]
ELSE 'abort'
IF equal THEN
compare the 2nd incoming character to the contents password[1)
ELSE 'abort'
If equal THEN
compare 3rd,4th,5th,6th.
ELSE 'abort'

IF any compare fails,t hen exit the 'get-password_and test' function, IE abort.

Now you will require a 'sync' byte so the PIC knows where the password begins.

sorry , no code, there are examples in the code library here and it's always best to 'learn while doing'. Start with one test, then build upon that. code will have 1 IF..THEN per password character.


Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group